Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取字典值中对象的完整列表_C#_Linq_Dictionary - Fatal编程技术网

C# 获取字典值中对象的完整列表

C# 获取字典值中对象的完整列表,c#,linq,dictionary,C#,Linq,Dictionary,我有这样的口述: private Dictionary<int, ICar> _ICarsDic; private Dictionary\u ICarsDic; 对象ICar实际上包含另一个对象列表: public interface ICar { int carId { get; set; } string carName { get; set; } List<IBrandsDetails> brandsDetails { get; set

我有这样的口述:

private Dictionary<int, ICar> _ICarsDic;
private Dictionary\u ICarsDic;
对象ICar实际上包含另一个对象列表:

public interface ICar 
{
    int carId { get; set; }
    string carName { get; set; }
    List<IBrandsDetails> brandsDetails { get; set; }
}
公共接口ICar
{
int carId{get;set;}
字符串carName{get;set;}
列表brandsDetails{get;set;}
}
我想编写一个函数,在单个列表中返回我所有
\u ICarsDic
值中的所有品牌详细信息

我相信LINQ会这么做(我不想写一个难看的循环),但我是新手,所以非常感谢您的帮助。谢谢

使用

List List=\u ICarsDic.SelectMany(r=>r.Value.brandsDetails)
.ToList();
使用

List List=\u ICarsDic.SelectMany(r=>r.Value.brandsDetails)
.ToList();

您可以将linq编写为

var listOfObjects = _ICarsDic.SelectMany(t => t.Value.brandsDetails).ToList();

您可以将linq编写为

var listOfObjects = _ICarsDic.SelectMany(t => t.Value.brandsDetails).ToList();

我想应该是
List
@yogi,不,它将返回List,@yogi,这正好说明了
Select(…)
SelectMany(…)
之间的区别。只需
选择
您就会得到一个
IEnumerable
,它将成为
列表
。但是使用
SelectMany
将所有序列连接成一个(非嵌套)序列。哇,你太快了!看来我真的很接近了,但是托利斯特()部分:-)谢谢!假设IBrandDetails现在是一个词汇表,我仍然需要获取它的值。你会怎么做?词汇品牌详细信息{get;set;}我想应该是
List
@yogi,不,它将返回List,@yogi,它正好说明了
Select(…)
SelectMany(…)
之间的区别。只需
选择
您就会得到一个
IEnumerable
,它将成为
列表
。但是使用
SelectMany
将所有序列连接成一个(非嵌套)序列。哇,你太快了!看来我真的很接近了,但是托利斯特()部分:-)谢谢!假设IBrandDetails现在是一个词汇表,我仍然需要获取它的值。你会怎么做?词汇表brandsDetails{get;set;}
var listOfObjects = _ICarsDic.SelectMany(t => t.Value.brandsDetails).ToList();