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

查看:146
本文介绍了在列表中获取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中获取所有distinctC.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆