Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq-根据字符串列表查询查询结果_C#_Linq - Fatal编程技术网

C# Linq-根据字符串列表查询查询结果

C# Linq-根据字符串列表查询查询结果,c#,linq,C#,Linq,我有一个字符串列表 List<string> firstNames = new List<string>(); 如何根据字符串列表查询原始结果联系人 select * from contacts where contacts.firstname like firstNames 差不多 firstNames.Any(x=>x.contacts.Contains(x)) 您可能应该将名字放入数据库查询中,如下所示: var cont

我有一个字符串列表

List<string> firstNames = new List<string>();                
如何根据字符串列表查询原始结果联系人

select * from contacts where contacts.firstname like firstNames
差不多

firstNames.Any(x=>x.contacts.Contains(x))

您可能应该将名字放入数据库查询中,如下所示:

var contacts = dataContext.Contacts.Where(c => firstNames.Contains(c.FirstName));
    DatabaseDataContext db = new DatabaseDataContext();
    List<string> firstNames = new List<string>();

    //--- loop through names and build a where query
    string WhereClause = string.Empty;
    for (int i = 0; i < firstNames.Count(); i++)
    {
        string s = firstNames[i].ToLower();

        if (i != Communities.Length - 1)
            WhereClause += "FirstName.ToLower().Contains(\"" + s + "\") OR "; //--- first name is the field name in the db
        else
            WhereClause += "FirstName.ToLower().Contains(\"" + s + "\")";
    }

    //--- execute query and pass the dynamic where clause
    IQueryable contacts = db.Contacts
                          .Where(WhereClause)
                          .OrderBy("FirstName");
现在,您在示例查询中使用了like,因此可能需要:

var contacts = dataContext.Contacts
                     .Where(c => firstNames.Any(f => c.FirstName.Contains(f)));
如果你能提供一些具体的例子,这会有所帮助

编辑:如果您真的要将数据库中已经存在的所有联系人(例如联系人列表)收回,则可以使用:

var matchingContacts = from contact in contacts
                       join name in firstNames
                       on contact.FirstName equals name
                       select contact;

首先,访问此链接,下载课程并将其添加到您的App_代码文件夹:

然后确保使用System.Linq.Dynamic添加

然后按如下方式构造查询:

var contacts = dataContext.Contacts.Where(c => firstNames.Contains(c.FirstName));
    DatabaseDataContext db = new DatabaseDataContext();
    List<string> firstNames = new List<string>();

    //--- loop through names and build a where query
    string WhereClause = string.Empty;
    for (int i = 0; i < firstNames.Count(); i++)
    {
        string s = firstNames[i].ToLower();

        if (i != Communities.Length - 1)
            WhereClause += "FirstName.ToLower().Contains(\"" + s + "\") OR "; //--- first name is the field name in the db
        else
            WhereClause += "FirstName.ToLower().Contains(\"" + s + "\")";
    }

    //--- execute query and pass the dynamic where clause
    IQueryable contacts = db.Contacts
                          .Where(WhereClause)
                          .OrderBy("FirstName");

祝你好运

由于混合上下文数据库和内存,我在做这样的事情时遇到了问题。是否不需要将这两个序列都放到一个公共上下文中?@JoelEtherton:这取决于您的操作,但将列表传递到数据库查询中是相当常见的。最重要的是要正确地处理它,这样你就不会把整个数据库带到客户端。小心使用“Contains”。它可以翻译为从t中选择*,其中x在'a','b','c'。。。。但是大多数SQL实现都限制了列表的长度。它可能有几十个或一百个项目,但肯定不是数千@奥利维耶雅科特·德斯科姆布斯:的确。如果数据库中只有几个联系人,但列表中有很多姓名,那么显然最好是将所有联系人都拉出来。如果列表中有很多姓名,数据库中有很多联系人,生活会变得更加艰难……我想查询原始查询,这样我只需访问数据库一次。