C# 通用列表 - 子数组上的 FindAll
我试图找到使用 C# (3.5) 过滤列表的最佳方法,但我似乎找不到任何与我的情况类似的示例。我愿意使用 lambda 或 linq。
与我发现的大多数示例相比,独特之处在于我的列表项每个都有子数组,例如...
var employees= new List<Employee>
{
new Employee{Name = "John",Nicknames="'J','Big J','Big John'"},
new Employee{Name = "Joshua",Nicknames="'J','Josh','Joshman'"},
}
然后我想过滤该列表,如下所示...
//using linq
var matchesByNickname =
from worker in employees
where worker.Nicknames.Equals("J")
select worker;
//or lambda
var employees2 = employees
.Where(e => Nicknames.Exists(n => n.Nickname == "J"))
但是当然,因为昵称本身就是一个数组,我不能使用 .Equals 或 .Contains 等。过滤此类列表的最佳方法是什么?
更新: 为了让我的例子保持简单,我有点误导了你。列表项上确实有真正的对象数组,而不是字符串。我的真实示例是自定义产品对象的列表。产品对象有一个 Regions 属性,它是 Region 对象的列表。产品可以没有区域、可以有 1 个区域、也可以有 1 个以上区域。区域对象有一个名称和一个 ID。 所以我真正想要的是过滤产品列表,以查找分配给特定区域的任何产品。
I'm trying to find the best approach to filtering a list with C# (3.5) but I can't seem to find any examples that are similar to my situation. I'm open to using lambdas or linq.
The thing that is unique compared to most of the examples that I've found, is that my list items each have child arrays for example...
var employees= new List<Employee>
{
new Employee{Name = "John",Nicknames="'J','Big J','Big John'"},
new Employee{Name = "Joshua",Nicknames="'J','Josh','Joshman'"},
}
I'd then like to filter that list something like this...
//using linq
var matchesByNickname =
from worker in employees
where worker.Nicknames.Equals("J")
select worker;
//or lambda
var employees2 = employees
.Where(e => Nicknames.Exists(n => n.Nickname == "J"))
But of course since Nicknames is itself an array I cannot use .Equals or .Contains etc. What would be the best approach to filtering a list of this type?
UPDATE:
In trying to keep my example simple I've misled you a bit. The list items do have true object arrays on them, not strings. My real world example is a list of custom product objects. The product object has a Regions property on it which is a list of Region objects. A product can have none, 1, or more than 1 region. The region object has a name and an id.
So what I really want is to filter a list of products, for any product assigned to a certain region.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的示例中,
Nickname
在我看来不是一个数组。这是一个字符串。假设它是一个字符串数组,您可以:如果您想保持原样(字符串),您可以尝试以下操作:
在上述两种情况下,您可以将
Where
替换为FindAll
并将结果返回到List
而不是IEnumerable
(List.FindAll< /code> 自 2.0 以来就已存在,但
Enumerable.Where
是 3.5 中的新增内容)。当然,理想情况下,您将使其成为如下所示的数组或列表,并使用第一种方法:
更新:您的实际示例与此示例案例没有太大不同。我认为您正在寻找
Any
而不是Contains
,因为您正在检查属性,而不是整个对象的相等性:In your example,
Nickname
is not an array as I see it. It's a string. Assuming it's an array of strings, you could:If you want to keep it as is (string), you can try something along the lines of:
In both of the cases above, you can replace
Where
withFindAll
and get results back in aList<T>
instead of anIEnumerable<T>
(List<T>.FindAll
has been around since 2.0 butEnumerable.Where
is new in 3.5).Of course, ideally, you'll make it an array or list like the following and use the first method:
UPDATE: Your real world example is not so much different from this sample case. I think you're looking for
Any
instead ofContains
since you are checking a property, not the whole object for equality:如果昵称周围有单引号,您可以查看单引号名称的字符串。
编辑:忘记单引号...和>=
If you have the single quotes around the nicknames, you could just peek into the string for the single quoted name.
Edit: Forgot single quotes...and >=