简体   繁体   English

我可以使用Linq以比这更优雅的方式从集合中获取属性吗?

[英]Can I get a property from a collection using Linq in a more elegant way than this?

I have a collection of items 我有一些物品

List<Thing> MyListOfThings

I get this list from a db using PetaPoco like this 我像这样使用PetaPoco从数据库中获得此​​列表

var MyListOfThings = db.Fetch<Thing>();

Then I want to get the value of a property like this: 然后,我想获得像这样的属性的值:

otherObject.Value = MyListOfThings.SingleOrDefault(q => q.UniqueID == otherValue).SomeProperty;

I think this is a very elegant solution, the problem is if the collection does not contain an object with the UniqueID of otherValue . 我认为这是一个非常优雅的解决方案,问题是如果集合中不包含具有otherValueUniqueIDotherValue Then it goes BANG with a null reference exception. 然后它以无效引用异常进入BANG。

The solution I have been using is 我一直在使用的解决方案是

otherObject.Value = MyListOfThings.SingleOrDefault(q => q.UniqueID == otherValue) == null
  ? ""
  : MyListOfThings.SingleOrDefault(q => q.UniqueID == otherValue).SomeProperty;

This works, Resharper still complains that there is a possible NullReferenceException, but I guess that's just because it doesn't count the first line as a nullcheck. 这有效,Resharper仍然抱怨可能存在NullReferenceException,但是我想那仅仅是因为它没有将第一行计为nullcheck。

However... this looks ugly and is so unDRY that it's dripping... 但是...看起来很丑而且很干燥以至于滴下...

Is there a more elegant solution for this? 有没有更优雅的解决方案呢?

It looks like SomeProperty is a string , and you want string.Empty as a fallback value. 看起来SomePropertystring ,并且您想要string.Empty作为后备值。 If that's right, then you can do 如果是这样,那么您可以

otherObject.Value = 
    (from thing in MyListOfThings
    where thing.UniqueID == otherValue
    select thing.SomeProperty)
        .SingleOrDefault() ?? string.Empty;

The LINQ expression is an IEnumerable<string> containing the SomeProperty value of all Thing s having the relevant UniqueID ; LINQ表达式是IEnumerable<string>包含具有相关UniqueID的所有ThingSomeProperty值; this might be an empty enumerable. 这可能是一个空的枚举。

Use a where statement first and use DefaultIfEmpty to set your fallback value 首先使用where语句,然后使用DefaultIfEmpty设置后备值

otherObject.Value = MyListOfThings
    .Where(q => q.UniqueID == otherValue)
    .Select(q => q.SomeProperty)
    .DefaultIfEmpty("")
    .SingleOrDefault();

Do it like this 像这样做

MyListOfThings object = MyListOfThings.where(q => q.UniqueID == otherValue).FirstOrDefault();

This will either get an object of my thing or null 这将得到我的东西的对象或为null

if(!object.IsNull) // null check so wont Bang
{
//assign value here
}

暂无
暂无

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

相关问题 是否有使用LINQ将项目添加到集合属性的更简洁的方法? - Is there a more succinct way of adding items to a collection property using LINQ? 在LINQ中是否有任何优雅的方法可以将集合基于属性存储到一组列表中 - Is there any elegant way in LINQ to bucket a collection into a set of lists based on a property 使用Linq选择多个属性 - Select more than one property using Linq 如何获得列表中多于一列的总计并将其使用LINQ放入对象中? - How can I get totals for more than one column in a list and put this into an object using LINQ? 我可以使用LINQ从Quickbooks Online ServiceContext取回100多个记录吗? - Can I use LINQ to get more than 100 records back from a Quickbooks Online ServiceContext? 使用LINQ,如何在部分字符串属性上过滤集合? - Using LINQ, how can I filter a collection on partial string property? 从集合中通过索引获取一组项目的最优雅方法是什么? - What is the most elegant way to get a set of items by index from a collection? 如何从对象列表到数组获得多个属性? - How can I get more than one property from an object list to an array? 有没有更优雅的方法可以使用 system.text.json 从 JSON object 获得特定值 - Is there a more elegant way to get specific value from JSON object using system.text.json 如何使用linq和lambdas从一个系列中获得前三名球员和他们的高分 - How can I get the top three players and their high scores from a collection using linq and lambdas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM