在列表中获取List的不同值 [英] Get distinct values of List in List
问题描述
我正在努力使用db应用程序,我需要在其中获取不同的值.整个结构看起来像这样
I'm struggling with a db application, where I need to get the distinct values. The whole structure looks somewhat like this
class A
{
public virtual ICollection<B> Bs { get; set; }
}
class B
{
public virtual C C { get; set; }
}
class C
{
public int x {get; set;}
}
这是数据库中的模型,我有所有A's
的子集.现在,我需要从所有A's
中获取所有distinct
值C.x
.
所以基本上像
This is the model from a db and I have a subset of all A's
. Now I need to get all distinct
values C.x
from all those A's
.
So basically something like
db.A.Where(something).SelectMany(s => s.Bs.SelectMany(t => t.C.x).ToList().Distinct()).ToList()
但是它告诉我,第二个SelectMany
不能从用法中推断出来.
But it tells me, that the second SelectMany
cannot be inferred from the usage.
推荐答案
使用以下方法修复代码:
Fix your code like this:
SelectMany(s => s.Bs.Select(t => t.C.x)
请注意,B
不是A
的成员,而是Bs
,第二个SelectMany
也应该是Select
.
Note that B
is not a member of A
but Bs
, and also your second SelectMany
should be Select
.
了解更多信息 https://stackoverflow.com/a/25564528/2946329
这篇关于在列表中获取List的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!