Lambda 表达式“IEnumerable” C#+其中包含
在 C# 中,我有一个名为 items
的 IEnumerable
Data
是一个包含 3 个字符串属性的类。
我想获取 attribute1 包含字符串的所有项目
public IEnumerable<Data> getDataFiltered(IEnumerable<Data> items,string Filter)
{
return items.Where(item=>item.Attribute1.Contains(Filter));
}
它会引发异常,但是当我使用:
public IEnumerable<Data> getDataFiltered(IEnumerable<Data> items,string Filter)
{
return items.Where(item=>item.Attribute1==Filter);
}
它有效。
我错过了什么吗?
In C#, I have an IEnumerable<Data>
named items
And Data
is a class that contains 3 string Attributes.
I want to get all items that attribute1 contains a string
public IEnumerable<Data> getDataFiltered(IEnumerable<Data> items,string Filter)
{
return items.Where(item=>item.Attribute1.Contains(Filter));
}
It throws an exception, however when I use:
public IEnumerable<Data> getDataFiltered(IEnumerable<Data> items,string Filter)
{
return items.Where(item=>item.Attribute1==Filter);
}
It works.
Did I miss something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您还应该主动过滤掉 Attribute1 具有空值的结果。
当您创建某个类型的变量时,除非您指定一个值,否则它将具有该类型的默认值。 .NET中有引用类型和值类型两种类型。引用类型(作为类创建的任何内容)的默认值为 null。字符串是引用类型的一个示例。
上面的代码中,所有三个属性值均未初始化并且值为 NULL。要在使用“new Data()”创建对象的新实例时为三个属性分配值,您可以创建一个分配值的构造函数。
现在所有属性都将具有空字符串值。这意味着它们已初始化但没有值。
值类型(定义为结构的任何内容)不能为 NULL。例如
DateTime
、int
、double
、decimal
等。数字类型的默认值为 0DateTime
的默认值为DateTime.MinValue
。You should also actively filter out the results that have a null value for Attribute1
When you create a variable of a type, unless you specify a value, it will have the default value for the type. There are two types in .NET reference types and value types. The default value for references types (anything created as a class) is null. A string is an example of a reference type.
The code above, all three attribute values are not initialized and have a value of NULL. To assign a value to the three attributes when create a new instance of your object with 'new Data()' you can create a constructor that assigns a value.
Now all attributes will have the empty string value. Meaning they are initialized but don't have a value.
Value types (anything defined as a struct) can not be NULL. Examples of this are
DateTime
,int
,double
,decimal
, etc. The default value for number types is 0. The default value forDateTime
isDateTime.MinValue
.我假设 Attribute1 的值为 null,然后调用 .Contains 将崩溃,而 == 则不会。
I assume the value of Attribute1 was null then calling .Contains will crash and == will not.
您的某些
Attribute1
可能为 null。您无法对
null
调用Contains
。Some of your
Attribute1
s are probably null.You can't call
Contains
onnull
.考虑一下:
我添加了空值检查,以确保不会在空引用上调用
Contains
。String
是一种引用类型,这意味着它有时可能为 null。Consdider this:
I added the nullity checks to make sure that
Contains
isn't called on null reference.String
is a reference type which means that it can occasionaly be null.如果您想检查确切的值,请使用 == 如果您想检查字符串是否出现,请使用以下任一方法。
对于区分大小写的方法如下:-
??是c#中的coalesc运算符,就像sql一样,即第一个非值
包含,但是区分大小写,因此如果不需要这样做,那么以下可能就是您想要的。
If you want to check for the exact value use == if you want to check for the occurence of a string use either of the following.
For a case sensitive approach the following :-
?? is the coalesc operator in c# just like sql's ie the first non value
Contains however is case sensitive so if this is not desired then the following might be what you want.