LINQ动态查询[英] LINQ dynamic query

本文是小编为大家收集整理的关于LINQ动态查询的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何动态生成linq查询:

int[] IDArray = {55, 36};

public IQueryable<ContactListView> FindAllContacts(int loggedUserID, int[] IDArray)
{
    var result = (
                 from contact in db.Contacts

                 //Start of dynamic part...
                 where 
                 contact.UserID == loggedUserID
                 foreach (var id in IDArray)
                 {
                     where contact.UserID == id 
                 }
                 // End of dynamic part

                 orderby contact.ContactID descending                         
                 select new ContactListView
                 {
                     ContactID = contact.ContactID,
                     FirstName = contact.FirstName,
                     LastName = contact.LastName                            
                 });

    return result;
}

谢谢,
Ile

推荐答案

您不能只使用Contains吗?我假设您要生成一个具有IN子句的查询. There's an example of that here: 用Linq到SQL .

在查询中创建
int[] IDArray = {55, 36};

public IQueryable<ContactListView> FindAllContacts(int loggedUserID, int[] IDArray)
{
    var result = (
                 from contact in db.Contacts
                 where contact.UserID == loggedUserID
                     && IDArray.Contains( contact.UserID )
                 orderby contact.ContactID descending                         
                 select new ContactListView
                 {
                     ContactID = contact.ContactID,
                     FirstName = contact.FirstName,
                     LastName = contact.LastName                            
                 });

    return result;
}

其他推荐答案

您可能想要where IDArray.Contains(contact.UserID)

本文地址:https://www.itbaoku.cn/post/1557029.html

问题描述

How to dynamically generate LINQ query:

int[] IDArray = {55, 36};

public IQueryable<ContactListView> FindAllContacts(int loggedUserID, int[] IDArray)
{
    var result = (
                 from contact in db.Contacts

                 //Start of dynamic part...
                 where 
                 contact.UserID == loggedUserID
                 foreach (var id in IDArray)
                 {
                     where contact.UserID == id 
                 }
                 // End of dynamic part

                 orderby contact.ContactID descending                         
                 select new ContactListView
                 {
                     ContactID = contact.ContactID,
                     FirstName = contact.FirstName,
                     LastName = contact.LastName                            
                 });

    return result;
}

Thanks,
Ile

推荐答案

Can't you just use Contains? I assume you want to generate a query that has an IN clause. There's an example of that here: Creating in queries with linq to sql.

int[] IDArray = {55, 36};

public IQueryable<ContactListView> FindAllContacts(int loggedUserID, int[] IDArray)
{
    var result = (
                 from contact in db.Contacts
                 where contact.UserID == loggedUserID
                     && IDArray.Contains( contact.UserID )
                 orderby contact.ContactID descending                         
                 select new ContactListView
                 {
                     ContactID = contact.ContactID,
                     FirstName = contact.FirstName,
                     LastName = contact.LastName                            
                 });

    return result;
}

其他推荐答案

You probably want where IDArray.Contains(contact.UserID)