Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search any property from a table with linq?

Tags:

c#

.net

linq

I need to search a dblinq table so that the function should return the list of rows if any of the fields matches the search input.

It is more similar to google search where you search with any keyword and the list of matching documents are displayed.

I used reflection for this technique. Can you please check if this method is correct or is there any simple method to achieve this?

I have got all the properties of the dblinq table rows and compared if its string type and then checked the value.

The code I have used

  public static List<Contacts> SearchContact(string searchText)
    {

        List<Contacts> list = new List<Contacts>();
        try
        {
            var rows= from p in context.tblContacts select p;

            foreach (var contact in rows)
            {
                Type type = contact.GetType();
                Type typesearch = searchText.GetType();
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo pro in properties)
                {
                    if (pro.PropertyType == typesearch)
                    {

                        var value = pro.GetValue(contact, null);
                        string val = value as string;

                            if (val != null && val == searchText)
                            {
                                list.Add(contact);
                                break;
                            }

                    }
                }
            }
        }
        catch (Exception ex)
        {
        }
        return list;
    }

PS: I am getting expected output from this method but I would like to know if I have any alternative way too.

like image 640
Coder323 Avatar asked Jul 26 '11 01:07

Coder323


1 Answers

I have no Visual Studio by hand to test it but this should work, too:

var contactProperties = typeof(Contact).GetProperties()
                               .Where(x => x.PropertyType == typeof(string))
                               .ToArray();

from contact in context.tblContacts
from property in contactProperties
let value = property.GetValue(contact, null) as string
where value == searchText
select contact

I would get the properties only once because it's the same every time.

On your question whether to to use reflection or not.. it depends. If your Contact type has only a few (2-5) string properties I would implement this method by hand instead of using reflection. It may be a bit tedious but pays of in readability, speed (however should not be a notable difference) and you can control which properties get compared (maybe your Contact contains a string property which should not be searched by).

If you are planning to extend/change the Contact type or have to check a big chunk of properties I would go with reflection.

like image 109
Zebi Avatar answered Nov 07 '22 03:11

Zebi