Click here to Skip to main content
15,899,937 members
Home / Discussions / C#
   

C#

 
GeneralRe: need code for project Pin
Eddy Vluggen28-Oct-14 9:40
professionalEddy Vluggen28-Oct-14 9:40 
GeneralRe: need code for project Pin
OriginalGriff28-Oct-14 9:41
mveOriginalGriff28-Oct-14 9:41 
AnswerRe: need code for project Pin
OriginalGriff28-Oct-14 9:40
mveOriginalGriff28-Oct-14 9:40 
GeneralRe: need code for project Pin
Pete O'Hanlon28-Oct-14 9:42
mvePete O'Hanlon28-Oct-14 9:42 
GeneralRe: need code for project Pin
OriginalGriff28-Oct-14 9:43
mveOriginalGriff28-Oct-14 9:43 
QuestionHow Can I Read Z- Segment In HL7 result message using Nhapi (ZPS)...? Pin
Member 1118592328-Oct-14 2:45
Member 1118592328-Oct-14 2:45 
AnswerRe: How Can I Read Z- Segment In HL7 result message using Nhapi (ZPS)...? Pin
Pete O'Hanlon28-Oct-14 3:03
mvePete O'Hanlon28-Oct-14 3:03 
AnswerRe: How Can I Read Z- Segment In HL7 result message using Nhapi (ZPS)...? Pin
BillWoodruff28-Oct-14 3:31
professionalBillWoodruff28-Oct-14 3:31 
AnswerRe: How Can I Read Z- Segment In HL7 result message using Nhapi (ZPS)...? Pin
Bernhard Hiller28-Oct-14 22:16
Bernhard Hiller28-Oct-14 22:16 
Questionword Wrapping in richtextbox using C# Pin
verenaJam27-Oct-14 22:56
verenaJam27-Oct-14 22:56 
AnswerRe: word Wrapping in richtextbox using C# Pin
BillWoodruff28-Oct-14 1:07
professionalBillWoodruff28-Oct-14 1:07 
GeneralRe: word Wrapping in richtextbox using C# Pin
verenaJam28-Oct-14 18:25
verenaJam28-Oct-14 18:25 
GeneralRe: word Wrapping in richtextbox using C# Pin
BillWoodruff28-Oct-14 20:57
professionalBillWoodruff28-Oct-14 20:57 
AnswerRe: word Wrapping in richtextbox using C# Pin
Vitor Breno28-Oct-14 9:04
Vitor Breno28-Oct-14 9:04 
GeneralRe: word Wrapping in richtextbox using C# Pin
verenaJam28-Oct-14 18:18
verenaJam28-Oct-14 18:18 
SuggestionMaking charts with pdfsharp Pin
Mol4ok27-Oct-14 5:30
Mol4ok27-Oct-14 5:30 
GeneralRe: Making charts with pdfsharp Pin
Richard Deeming27-Oct-14 9:49
mveRichard Deeming27-Oct-14 9:49 
GeneralRe: Making charts with pdfsharp Pin
Mol4ok27-Oct-14 17:09
Mol4ok27-Oct-14 17:09 
GeneralRe: Making charts with pdfsharp Pin
Richard Deeming28-Oct-14 2:12
mveRichard Deeming28-Oct-14 2:12 
GeneralRe: Making charts with pdfsharp Pin
Mol4ok28-Oct-14 3:24
Mol4ok28-Oct-14 3:24 
GeneralRe: Making charts with pdfsharp Pin
Gerry Schmitz27-Oct-14 22:35
mveGerry Schmitz27-Oct-14 22:35 
GeneralRe: Making charts with pdfsharp Pin
Mol4ok28-Oct-14 2:11
Mol4ok28-Oct-14 2:11 
GeneralRe: Making charts with pdfsharp Pin
Gerry Schmitz28-Oct-14 9:45
mveGerry Schmitz28-Oct-14 9:45 
QuestionExpression to Check for a String Value in All Properties of a Type Pin
Agent__00727-Oct-14 1:42
professionalAgent__00727-Oct-14 1:42 
I am trying to write a small search functionality in LINQ-to-SQL (with Sharp Repository and Entity Framework) where given a string value, I should be able to search it through all properties and return the entities which have the input string value "Contained" (as in someString.Contains) in any of their properties (with "searchable" types like int, long, etc since search by ID should be possible).

I have written a method which does this for a "single" string property (which was the requirement earlier Sigh | :sigh: ) and returns an Expression predicate which I pass to my SharpRepository instance as the selector.
C#
public static Expression<Func<T, bool>> ContainsExpression<T>(string propertyName, string propertyValue)
        {
            var parameterExpression = Expression.Parameter(typeof(T), typeof(T).Name); // type => 

            var propertyExpression = Expression.PropertyOrField(parameterExpression, propertyName); // type.Name
            var stringTypeArray = new[] { typeof(String) };
            var containsMethod = typeof(String).GetMethod("Contains", stringTypeArray); // Contains method
            var valueToBeChecked = Expression.Constant(propertyValue, typeof(String)); // property value as a constant

            var finalContainsExpression = Expression.Call(propertyExpression, containsMethod, valueToBeChecked); // type.Name.Contains(propertyValue)

            return Expression.Lambda<Func<T, bool>>(finalContainsExpression, parameterExpression);
        }


I have even tried overriding ToString() method in my entity (simple Employee type at the moment) as:
C#
public override string ToString()
        {
            string instance = String.Concat(Id, ",", Name, ",", Department, ",", DateOfJoining.ToString("dd-MMM-yyyy"), ",", Salary);
            return instance;
        }
and then generate an Expression as:
C#
Employee => Employee.ToString().Contains(ValueToBeSearched)

But needless to say, the first one won't work for the types other than String (since I will need to do someTypeOtherThanString.ToString().Contains(..) which is not supported in LINQ-to-SQL and the later one also won't work due to the similar reason!

I do know that I can simply write an SP for this functionality, I want to know if there's a way to achieve this using LINQ-to-SQL. Also, I need to pass the Expression as the selector to my SharpRepository instance so although I am not sure if a .ToList<T>() on all entities followed by a filter expression will work, unfortunately I am not open to that, I would rather write an SP.

[EDIT]
Just came across SqlFunctions[^].StringConvert() method overloads. Unfortunately they only support numeric types. But I think I have the necessary things to go ahead with string and numeric types. Will see what could be done for rest of the types, if they exist at all and should be search-enabled.
[/EDIT]


Any pointers will be appreciated. Thanks!
Your time will come, if you let it be right.


modified 28-Oct-14 1:58am.

AnswerRe: Expression to Check for a String Value in All Properties of a Type Pin
Eddy Vluggen28-Oct-14 9:09
professionalEddy Vluggen28-Oct-14 9:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.