开发者

List filtering with LINQ

So I came across this method in code:

private void FilterBasedUponPermission(List<Data.Indications.SpWeb_SavedIndications1LightDataObject> list)
        {
            list.RemoveAll(item =>
                (item.Permission == Controllers.Indications.ICConstants.TradeType_LLH && !isLLH) ||
                (item.Permission == Controllers.Indications.ICConstants.TradeType_ALM && !isALM) ||
                (item.Permission == Controllers.Indications.ICConstants.TradeType_RealEstate && !isRE) ||
                (item.Permission == Controllers.Indications.ICConstants.TradeType_Auditor && !isAuditor));
        }

However, in the list passed in, some of the permission values are null, and it's keeping them in the filtered list. I want it to remove the items that also don't have a permission set, not just the ones that 开发者_运维百科don't match the permission that you have as a user.

Thanks!


Just add a item.Permission == null

list.RemoveAll(item => item.Permission == null ||
                (item.Permission == Controllers.Indications.ICConstants.TradeType_LLH && !isLLH) ||
                (item.Permission == Controllers.Indications.ICConstants.TradeType_ALM && !isALM) ||
                (item.Permission == Controllers.Indications.ICConstants.TradeType_RealEstate && !isRE) ||
                (item.Permission == Controllers.Indications.ICConstants.TradeType_Auditor && !isAuditor));


private void FilterBasedUponPermission(List<Data.Indications.SpWeb_SavedIndications1LightDataObject> list)
        {
            list.RemoveAll(item =>
                (item.Permission == null) ||
                (item.Permission == Controllers.Indications.ICConstants.TradeType_LLH && !isLLH) ||
                (item.Permission == Controllers.Indications.ICConstants.TradeType_ALM && !isALM) ||
                (item.Permission == Controllers.Indications.ICConstants.TradeType_RealEstate && !isRE) ||
                (item.Permission == Controllers.Indications.ICConstants.TradeType_Auditor && !isAuditor));
        }


Does on of those work?

list.RemoveAll(item => !item.Permission.HasValue); // in case Permission is Nullable<T>
list.RemoveAll(item => item.Permission == null);

You can add all the other conditions after a logical or (||).

Cheers, Matthias

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜