如何使用Linq从List 中获取第一个对象
我在c#4.0中有以下代码。
//Dictionary object with Key as string and Value as List of Component type object Dictionary<String, List> dic = new Dictionary<String, List>(); //Here I am trying to do the loping for List foreach (List lstComp in dic.Values.ToList()) { // Below I am trying to get first component from the lstComp object. // Can we achieve same thing using LINQ? // Which one will give more performance as well as good object handling? Component depCountry = lstComp[0].ComponentValue("Dep"); }
尝试:
var firstElement = lstComp.First();
您也可以使用FirstOrDefault()
,以防lstComp
不包含任何项目。
http://msdn.microsoft.com/en-gb/library/bb340482(v=vs.100).aspx
编辑:
获取Component Value
:
var firstElement = lstComp.First().ComponentValue("Dep");
这将假设lstComp
有一个元素。 另一种更安全的方式是……
var firstOrDefault = lstComp.FirstOrDefault(); if (firstOrDefault != null) { var firstComponentValue = firstOrDefault.ComponentValue("Dep"); }
无论发生什么, [0]
或.First()
都会给你相同的表现。
但是你的Dictionary
可能包含IEnumerable
而不是List
,然后你就不能使用[]
运算符了。 这就是差异巨大的地方。
因此,对于您的示例,它并不重要,但对于此代码,您无法使用First():
var dic = new Dictionary>(); foreach (var components in dic.Values) { // you can't use [0] because components is an IEnumerable var firstComponent = components.First(); // be aware that it will throw an exception if components is empty. var depCountry = firstComponent.ComponentValue("Dep"); }
你可以做
Component depCountry = lstComp .Select(x => x.ComponentValue("Dep")) .FirstOrDefault();
或者,如果您希望将其用于整个值列表,您甚至可以将其绑定到键
var newDictionary = dic.Select(x => new { Key = x.Key, Value = x.Value.Select( y => { depCountry = y.ComponentValue("Dep") }).FirstOrDefault() } .Where(x => x.Value != null) .ToDictionary(x => x.Key, x => x.Value());
这会给你一本新词典。 您可以访问这些值
var myTest = newDictionary[key1].depCountry
你也可以用这个:
var firstOrDefault = lstComp.FirstOrDefault(); if(firstOrDefault != null) { //doSmth }
对于linq表达式,您可以像这样使用:
List list = new List () {1,2,3 }; var result = (from l in list select l).FirstOrDefault();
对于lambda表达式,您可以像这样使用
List list = new List(){1,2,3}; int x = list.FirstOrDefault();
有很多这样的方法:
.First .FirstOrDefault .Single .SingleOrDefault
选择最适合你的。
var firstObjectsOfValues = (from d in dic select d.Value[0].ComponentValue("Dep"));
我愿意这样:
//Dictionary object with Key as string and Value as List of Component type object Dictionary> dic = new Dictionary>(); //from each element of the dictionary select first component if any IEnumerable components = dic.Where(kvp => kvp.Value.Any()).Select(kvp => (kvp.Value.First() as Component).ComponentValue("Dep"));
但只有当它确定列表仅包含Component类或子对象的对象时
试试这个以获取所有列表,然后是你想要的元素(比如你的第一个):
var desiredElementCompoundValueList = new List(); dic.Values.ToList().ForEach( elem => { desiredElementCompoundValue.Add(elem.ComponentValue("Dep")); }); var x = desiredElementCompoundValueList.FirstOrDefault();
要直接获得第一个元素值而不需要大量的foreach迭代和变量赋值:
var desiredCompoundValue = dic.Values.ToList().Select( elem => elem.CompoundValue("Dep")).FirstOrDefault();
看看两种方法之间的区别:在第一种方法中,您通过ForEach获得列表,然后是您的元素。 在第二种情况下,您可以直接获得价值。
相同的结果,不同的计算;)
我这样做了。
List