第二个Linq过滤器可满足多种条件 [英] Second Linq filter for multiple conditions

查看:106
本文介绍了第二个Linq过滤器可满足多种条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(简化的)课程,如下所示:

I have a class (simplified) as follows:

class AppMeta
{
    public int Id { get; set; }
    public string Scope { get; set; }
    public User CreatedBy { get; set; }
}

我创建了一个LINQ语句,该语句通过交叉引用整数列表并丢弃匹配项来正确过滤结果:

I've created a LINQ statement that properly filters results by cross referencing a list of integers, and discarding matches:

var hiddenApps = List<int>();
//populate hiddenApps
var listItems = List<rawdata>;
//populate listItems
List<AppMeta> apps = AppMeta.Collection(listItems).Where(i => !hiddenApps.Contains(i.Id)).ToList()

我的问题是,我需要按以下方式进一步过滤此列表,

My question is, I need to further filter this list as follows,

(Where Scope == "user" && Where User.LoginName == CurrentUser.LoginName)

我是否仍可以在一条Linq声明中做到这一点,即我可以将其与上面的行结合起来吗?最好的方法是什么?

Can I still do that in one Linq statement, I.E. Can I combine that with my line above? What's the best way to do this?

推荐答案

您可以在Where子句中指定多个条件.将Where子句修改为:

You can specify multiple conditions in your Where clause. Modify your Where clause as:

.Where(i => !hiddenApps.Contains(i.Id) 
            && i.Scope == "user" 
            && i.CreatedBy.LoginName == CurrentUser.LoginName )

因此您的查询将是:

List<AppMeta> apps = AppMeta.Collection(listItems)
                            .Where(i => !hiddenApps.Contains(i.Id) 
                                   && i.Scope == "user" 
                                   && i.CreatedBy.LoginName == CurrentUser.LoginName)
                            .ToList();

这篇关于第二个Linq过滤器可满足多种条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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