简体   繁体   English

创建一个LINQ查询比较数组

[英]Create a LINQ query comparing arrays

I'm very new to LINQ and I'm searching to filter an SQL view which has a column called 'Readers' containing several group names separated by '#' (es. "Administrators#HR Group#Employees Group"). 我是LINQ的新手,我正在寻找一个SQL视图的筛选器,该视图的列名为“读者”,其中包含多个以“#”(例如“ Administrators#HR Group#Employees Group”)分隔的组名。 Having a list of user's groups, I need to extract all the records where Readers contains at least one of the user's groups. 有一个用户组列表,我需要提取所有记录,其中Readers包含至少一个用户组。 In other words, the user must see only those records belonging to him. 换句话说,用户只能看到属于他的那些记录。

I found this solution, but I think it's extremely inefficient: 我找到了这个解决方案,但是我认为它效率极低:

private List<vwFax>getmyFaxes(List<string> myGroups)
{
    var myFax = db.vwFax.AsQueryable();
    var res = db.vwFax.AsQueryable();
    List<vwFax> outRes= new List<vwFax>();

    foreach (string elem in myGroups)
    {
        res = (from a in myFax
                where a.Readers.Contains(elem)
                select a); 
        if(res.Count() > 0)
        {
            outRes.AddRange(res);
        }               
    }
    return outRes.ToList();
}

Any help please? 有什么帮助吗?

Basically what you are saying in the following query is: For each item in myFax take it only if that item.Readers contains Any (at least 1) of the items in myGroups 基本上你是说在下面的查询是:对于每一个项目myFax把它只有当item.Readers包含Any (至少1)在项目myGroups

outRes = db.myFax.Where(item => myGroups.Any(grp => item.Readers.Contains(grp)));

and in query-syntax: 并在查询语法中:

outRes = from item in db.myFax
         where myGroups.Any(grp => item.Readers.Contains(grp))
         select item;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM