Click here to Skip to main content
15,888,283 members
Home / Discussions / C#
   

C#

 
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 
GeneralRe: Expression to Check for a String Value in All Properties of a Type Pin
Agent__00728-Oct-14 17:24
professionalAgent__00728-Oct-14 17:24 
GeneralRe: Expression to Check for a String Value in All Properties of a Type Pin
Eddy Vluggen29-Oct-14 8:57
professionalEddy Vluggen29-Oct-14 8:57 
AnswerRe: Expression to Check for a String Value in All Properties of a Type Pin
Alaric_28-Oct-14 10:22
professionalAlaric_28-Oct-14 10:22 
GeneralRe: Expression to Check for a String Value in All Properties of a Type Pin
Agent__00728-Oct-14 17:26
professionalAgent__00728-Oct-14 17:26 
AnswerRe: How I Got it (Partially) Done Pin
Agent__00728-Oct-14 17:08
professionalAgent__00728-Oct-14 17:08 
Questioninherit a observablecollection Pin
cicill27-Oct-14 0:39
cicill27-Oct-14 0:39 
AnswerRe: inherit a observablecollection Pin
BillWoodruff27-Oct-14 1:50
professionalBillWoodruff27-Oct-14 1:50 
QuestionExtraction of data from excel to databae Pin
Member 1118244626-Oct-14 21:38
Member 1118244626-Oct-14 21:38 
AnswerRe: Extraction of data from excel to databae Pin
Pete O'Hanlon26-Oct-14 21:41
mvePete O'Hanlon26-Oct-14 21:41 
GeneralRe: Extraction of data from excel to databae Pin
Member 1118244626-Oct-14 21:58
Member 1118244626-Oct-14 21:58 
Questionsimple code for game Pin
cicill26-Oct-14 15:23
cicill26-Oct-14 15:23 
AnswerRe: simple code for game Pin
BillWoodruff26-Oct-14 16:32
professionalBillWoodruff26-Oct-14 16:32 
AnswerRe: simple code for game Pin
Mycroft Holmes26-Oct-14 19:20
professionalMycroft Holmes26-Oct-14 19:20 
AnswerRe: simple code for game Pin
Pete O'Hanlon26-Oct-14 21:38
mvePete O'Hanlon26-Oct-14 21:38 
GeneralRe: simple code for game Pin
harold aptroot27-Oct-14 4:29
harold aptroot27-Oct-14 4:29 
QuestionData binding in wpf Pin
rajeevanagaraj25-Oct-14 2:16
rajeevanagaraj25-Oct-14 2:16 

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.