C# 检查 lambda 表达式中的属性是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1698684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:04:02  来源:igfitidea点击:

Check if property is null in lambda expression

c#listviewlambda

提问by mickyjtwin

I have a list of objects that I am trying to bind to a listview. I am sorting by two properties. The problem exists whereby some records may not have one of the properties. This is causing an error. I would like it to still bind the records that have the property.

我有一个我试图绑定到列表视图的对象列表。我按两个属性排序。存在的问题是某些记录可能不具有其中一项属性。这导致了错误。我希望它仍然绑定具有该属性的记录。

IEnumerable<ERec> list = retailerList.Cast<ERec>();
lvwRetailStores.DataSource = list.OrderByDescending(r => r.Properties["RS_Partner Type"].ToString())
                                 .ThenBy(r => r.Properties["RS_Title"].ToString());

采纳答案by Adam Says - Reinstate Monica

list.Where(r => r.Properties["RS_Partner_Type"] != null && r.Properties["RS_Title"] != null)
    .OrderByDescending(r => r.Properties["RS_Partner Type"].ToString())
    .ThenBy(r => r.Properties["RS_Title"].ToString());

Or instead of != null, use whatever test the Properties collection has.

或者代替 != null,使用 Properties 集合的任何测试。

回答by Serguei

You can use a ternary expression in the lambda:

您可以在 lambda 中使用三元表达式:

list.OrderByDescending(r => r.Properties["RS_Partner_Type"] == null ? null : r.Properties["RS_Partner Type"].ToString())
    .ThenBy(r => r.Properties["RS_Title"] == null ? null : r.Properties["RS_Title"].ToString());

回答by outis

Another common approach is to give the collection a suitable default value, and return that when the collection doesn't have a particular key. For instance, if Propertiesimplements IDictionary,

另一种常见的方法是为集合提供一个合适的默认值,并在集合没有特定键时返回该值。例如,如果Properties实现 IDictionary,

public static class IDictionaryExtension {
    public static TValue GetValue<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue default) {
        TValue result;
        return dict.TryGetValue(key, out result) ? result : dflt;
    }
}
...
lvwRetailStores.DataSource = list.OrderByDescending(r => r.GetValue("RS_Partner Type", "").ToString())
                                 .ThenBy(r => r.GetValue("RS_Title","").ToString());

回答by hiramknick

I've found that the ?? Operator works well. I use Parenthesis to evaluate for null,

我发现 ?? 操作员运作良好。我使用括号来评估空值,

For Example:

例如:

Datetime? Today = DateTimeValue // Check for Null, if Null put Today's date datetime GoodDate = Today ?? DateTime.Now

Datetime? Today = DateTimeValue // Check for Null, if Null put Today's date datetime GoodDate = Today ?? DateTime.Now

This same logic works in Lambda, just use parenthesis to ensure that the correct comparisons are used.

同样的逻辑在 Lambda 中也有效,只需使用括号来确保使用正确的比较。