开发者

List filtering with LINQ

开发者 https://www.devze.com 2023-03-11 08:29 出处:网络
So I came across this method in code: private void FilterBasedUponPermission(List<Data.Indications.SpWeb_SavedIndications1LightDataObject> list)

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

精彩评论

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