在列表中获取列表的不同值[英] Get distinct values of List in List

本文是小编为大家收集整理的关于在列表中获取列表的不同值的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在DB应用程序中挣扎,在这里我需要获得不同的值.整个结构看起来有点像这个

class A
{
    public virtual ICollection<B> Bs { get; set; }
}
class B
{
    public virtual C C { get; set; }
}
class C
{
    public int x {get; set;}
}

这是来自DB的模型,我有所有A's的子集.现在,我需要从所有这些A's中获取所有distinct值C.x. 所以基本上是

db.A.Where(something).SelectMany(s => s.Bs.SelectMany(t => t.C.x).ToList().Distinct()).ToList()

,但它告诉我,第二个SelectMany不能从用法中推断出来.

推荐答案

这样修复您的代码:

SelectMany(s => s.Bs.Select(t => t.C.x)

请注意,B不是A的成员,而是Bs,您的第二个SelectMany应该是Select.

.

阅读更多 https://stackoverflow.com/a/255564528/2946329

其他推荐答案

您可以尝试一下吗?我也认为需要添加.Include语句:

db.A.Where(something)/*.Include(a => a.Bs.Select(b => b.C))*/
    .SelectMany(x => x.Bs.Select(b => b.C.x)).Distinct();

本文地址:https://www.itbaoku.cn/post/1556981.html

问题描述

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;}
}

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()

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)

Note that B is not a member of A but Bs, and also your second SelectMany should be Select.

Read more https://stackoverflow.com/a/25564528/2946329

其他推荐答案

Could you try this? Also I think it'll be needed to add .Include statements:

db.A.Where(something)/*.Include(a => a.Bs.Select(b => b.C))*/
    .SelectMany(x => x.Bs.Select(b => b.C.x)).Distinct();