如何从使用LINQ一个表中搜索任何财产? [英] How to search any property from a table with linq?

查看:89
本文介绍了如何从使用LINQ一个表中搜索任何财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要寻找一个表的DBLinq这样函数应返回行的列表,如果任何字段匹配搜索输入。

这是更类似于谷歌搜索,你与任何关键字和匹配文档显示的搜索列表。

我曾经思考这种技术。能否请您检查,如果这种方法是正确的或者是有没有简单的方法来实现这一目标?

我已经得到的表的DBLinq行的所有属性和比较,如果它的字符串类型,然后检查的值。

的code我已经使用

 公共静态列表<联系和GT; SearchContact(字符串searchText)
    {

        名单<联系和GT;名单=新的名单,其中,联系和GT;();
        尝试
        {
            VAR行从P =在context.tblContacts选择磷;

            的foreach(行VAR联系)
            {
                类型类型= contact.GetType();
                键入typesearch = searchText.GetType();
                的PropertyInfo []属性= type.GetProperties();
                的foreach(的PropertyInfo PRO在性能)
                {
                    如果(pro.PropertyType == typesearch)
                    {

                        VAR值= pro.GetValue(触点,NULL);
                        字符串VAL =值串;

                            如果(VAL = NULL和放大器;!&放大器; VAL == searchText)
                            {
                                list.Add(接触);
                                打破;
                            }

                    }
                }
            }
        }
        赶上(例外前)
        {
        }
        返回列表;
    }
 

  

PS:我收到预期的输出从这个方法,但我想知道有没有任何其他方式太

解决方案

我没有Visual Studio的手工测试,但应该工作,太:

  VAR contactProperties = typeof运算(联系).GetProperties()
                               。凡(X => x.PropertyType == typeof运算(字符串))
                               .ToArray();

从context.tblContacts接触
从contactProperties物业
让值= property.GetValue(触点,空)的字符串
其中,value == searchText
选择联系人
 

我会得到的属性只有一次,因为每次是相同的。

在你的问题是否使用反射或不..它依赖。 如果你的联系方式类型只有少数(2-5)的字符串属性我会实现这个方法用手工而不是使用反射。它可能是一个有点乏味,但可读性支付的,速度(但不应该是一个显着的区别),你可以控制哪些属性得到比较(也许您的联系包含一个字符串属性,它不应该被搜索)。

如果您正计划扩大/修改联系方式键入或要检查属性的很大一部分我会与反思去。

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.

解决方案

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.

这篇关于如何从使用LINQ一个表中搜索任何财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆