简体   繁体   English

如何使用LINQ Lambda从IEnumerable集合中获取对象?

[英]How to Get a Object from IEnumerable collection using LINQ Lambda?

storageColl is having a IStorage with property "Id" as "Test". storageColl的IStorage具有属性“ Id”作为“ Test”。

What I am doing- 我在做什么-

string id="Test";
IEnumerable<IStorageObject> storageColl = getStorageCollection();
IStorageObject storageObject = storageColl.ToList().Where(m => m.Properties["Id"] == id)
.ToList()
.Cast<IStorageObject>().ToArray()[0];

Is there a better way to do this. 有一个更好的方法吗。 As this may throw array out of bound exception if the storageColl will not have that "Test". 因为如果storageColl没有该“测试”,则这可能会使数组超出范围的异常。

You can use FirstOrDefault on the IEnumerable . 您可以在IEnumerable上使用FirstOrDefault

var storageObject = storageCol1.Where(m => m.Properties["Id"] == id).FirstOrDefault();

Or as David Hedlund pointed out, use the predicate overload on FirstOrDefault and remove the Where. 或正如David Hedlund指出的那样,在FirstOrDefault上使用谓词重载并删除Where。

var storageObject = storageCol1.FirstOrDefault(m => m.Properties["Id"] == id);

Your storageColl is a sequence of objects that implement IStorageObject. 您的storageColl是实现IStorageObject的一系列对象。 The use of the Where only limits the elements you get when you enumerate over the sequence, it does not change them. 使用Where仅限制您枚举序列时获得的元素,不会更改它们。

It is a waste of processing power to convert the sequence to a list when you only need the first element of the sequence or the a subset of it. 当您只需要序列的第一个元素或其子集时,将序列转换为列表会浪费处理能力。

Familiarize yourself with the following Ling functions: 熟悉以下Ling函数:

  • Any() returns true if the sequence contains at least one element 如果序列包含至少一个元素,则Any()返回true
  • Any( item => ....) return true if any of the elements in the sequence meets the requirement 如果序列中的任何元素满足要求,则Any(item => ....)返回true
  • First() returns the first element of the sequence. First()返回序列的第一个元素。 Exception if not Any() 异常,如果不是Any()
  • FirstOrDefault returns the first element of the sequence or the default (usually null) if not Any() FirstOrDefault返回序列的第一个元素;如果不是Any(),则返回默认值(通常为null)

The nice thing about these functions is that they don't have to enumerate over all elements in the sequence, but can stop as soon as they found something. 这些函数的好处是,它们不必枚举序列中的所有元素,但是一旦找到它们就可以停止。

If you use ToList() the code enumerates over all elements, throws most of them away and uses only the first element. 如果使用ToList(),则代码将枚举所有元素,将其中的大部分扔掉,并仅使用第一个元素。 FirstOrDefault() would have stopped after the first enumeration. FirstOrDefault()将在第一次枚举后停止。

您可以通过以下方式简单地实现

var result = storageColl.Where(m => m.Properties["Id"] == id);

since the collection is implement IStorageObject you don't neet to cast them and for get item by index you can use any class that utilizes Array or IList 由于该集合是实现IStorageObject的实现,因此您无需对其进行强制转换,并且对于按索引获取项,可以使用任何利用Array或IList的类

since LINQ operates on IEnumerable (Array itself is enumerable->to iterate) you don't need to cast them to array.you can utilize ElementAt method or Use IList classes (as List) 由于LINQ在IEnumerable上运行(数组本身是可枚举的->进行迭代),因此无需将它们强制转换为数组。您可以使用ElementAt方法或使用IList类(作为List)

IStorageObject storageObject = storageColl.Where(m => m.Properties["Id"] == id).First();

You should check firstOrDefault because can return null. 您应该检查firstOrDefault因为它可以返回null。

var Object = storageCol.Where(p => p.Properties["Id"] == id).FirstOrDefault();

if(Object != null)
{
    // Do some Work
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM