简体   繁体   English

LINQ查询 - 根据具有与字符串匹配的属性的值选择键?

[英]LINQ Query - Selecting a key based upon the value having a property that matches a string?

I have an IDictionary 我有一个IDictionary

I need to select the first Foo where the Bar.Prop1 matches a string value. 我需要选择Bar.Prop1与字符串值匹配的第一个Foo。

public class Foo { }

public class Bar
{
    public String Prop1 { get; set; }
}

right now I have it like so... 现在我有这样的...

foreach (var kvp in MyDictionary)
{
    if (kvp.Value.Prop1 == theString)
    {
        var key = kvp.Key;
        //Do something with it
        break;
    }
}

But that just doesn't seem as clean as a LINQ Query is. 但这似乎并不像LINQ Query那样干净。 ReSharper turned it into: ReSharper把它变成了:

foreach (var kvp in MyDictionary.Where(kvp => kvp.Value.Prop1 == theString))
{
    var key = kvp.Key;
    //Do something with it
    //break; is unnecessary because I only get one kvp back anyways.
}

I only want the very first item that matches, because I don't ever expect to get back more than one KVP. 我只想要匹配的第一个项目,因为我不希望得到多个KVP。 That goes against the business logic, so Unit Testing takes care of that. 这违背了业务逻辑,因此单元测试负责这一点。

I only want the very first item that matches, because I don't ever expect to get back more than one KVP. 我只想要匹配的第一个项目,因为我不希望得到多个KVP。 That goes against the business logic, so Unit Testing takes care of that. 这违背了业务逻辑,因此单元测试负责这一点。

If this is the case, I would argue you need to use a stronger code guarantee of your intention, and that would be the Single (or SingleOrDefault ) method. 如果是这种情况,我认为你需要对你的意图使用更强的代码保证,那就是Single (或SingleOrDefault )方法。 First is going to return the first object of arbitrarily many that match a given predicate. First是返回与给定谓词匹配的任意多个第一个对象。 If many goes against your expectations and business rules, this seems to be an error. 如果许多违背您的期望和业务规则,这似乎是一个错误。 Treat it as such. 这样对待它。

var key = MyDictionary.Single(pair => pair.Value.Prop1 == someValue).Key;

With Single , if there is more than one matching item in a sequence, it will result in an exception. 使用Single ,如果序列中有多个匹配项,则会导致异常。

SingleOrDefault allows for 0 or 1, but never more. SingleOrDefault允许0或1,但永远不会更多。 If using this approach, you'd want to capture the result and compare to null before performing additional operations with it (firing methods, accessing properties, etc.). 如果使用此方法,您需要捕获结果并在执行其他操作(触发方法,访问属性等)之前与null进行比较。

var key = MyDictionary.First(kvp => kvp.Value.Prop1 == theString).Key;

@Bala R's answer is correct, but Anthony Pegram makes a very good point in his comment on the OP. @Bala R的回答是正确的,但Anthony Pegram在他对OP的评论中提出了一个非常好的观点。 If you're doing this more than a few times with different keys, what you should do instead is reverse the dictionary, so you don't have to traverse the entire collection every time you want a value. 如果您使用不同的键执行此操作多次,则应该执行的操作是反转字典,因此每次需要值时都不必遍历整个集合。

// Do this just once: it's expensive
var reverseDict = MyDictionary.ToDictionary(kvp => kvp.Value.Prop1, kvp => kvp.Key);

...
// Do this as many times as you need: it's cheap
var key = reverseDict[someValue];

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

相关问题 根据Linq查询中的整数属性值将值分配给字符串属性 - Assign value to String Property based on integer Property value in Linq query 根据导航属性值中的值选择LINQ - Selecting in LINQ based on value in navigation property value 将 KeyValuePairs 列表传递给 LINQ 查询并选择与实体 ID 匹配的键的值 - Passing List of KeyValuePairs to LINQ query and selecting the Value for the Key that matches the entity's Id 如何基于至少具有一个与字符串数组中的行匹配的条目的字段来限制linq查询中的行? - How can I restrict rows in a linq query based on a field having at least one entry that matches a row in a string array? 用于返回具有特定属性值的嵌套数组的 Linq 查询 - Linq Query to Return Nested Arrays having specific Property Value 在Linq投影中基于名称选择属性 - Selecting a property based on Name in Linq projection 更好的LINQ查询以根据条件设置属性值 - Better LINQ query to set value of property based on condition LINQ查询以基于属性选择 - LINQ query to select based on property 使用 Linq 查询选择属性的任何值与列表中的值匹配的对象 - Using a Linq query to select objects where any value of a property matches values from a list Linq查询比较2列表 <string> 用于不同的比赛 - Linq query to compare 2 List<string> for distinct matches
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM