简体   繁体   English

使用 LINQ 在 List 中查找特定值

[英]Using LINQ to find a specific value in a List

I'm trying to use LINQ to find a specific string value "ConstantA" in a List.我正在尝试使用 LINQ 在列表中查找特定的字符串值“ConstantA”。

Variable "stuff" is a type of:变量“stuff”是一种类型:

List<KeyValuePair<string, string>> 

The first value in the List (StuffName) is the one that I'm looking for:列表 (StuffName) 中的第一个值是我要查找的值:

在此处输入图像描述

How do I use LINQ to find the correct StuffName = "ConstantA"?如何使用 LINQ 找到正确的 StuffName = "ConstantA"?

This is my code below:这是我下面的代码:

var listOfStuff = CalculateStuff();
foreach (var stuff in listOfStuff)
{
  if (stuff.Constants.FindAll("ConstantA") //**I’m having problem with LINQ here**
  {
   …
  }
}


private static List<Stuff> CalculateStuff()
{
…
}

public class Stuff : IHasIntInst
{
    public List<KeyValuePair<string, string>> Constants { get; set; }
}

With your current version of code:使用您当前版本的代码:

var listOfStuff = CalculateStuff();
foreach (var stuff in listOfStuff)
{
    var items = stuff.Constants.FindAll((keyValuePair) => keyValuePair.Key == "ConstantA");
    if (items.Any())
    {
        //**I’m having problem with LINQ here**
    }
}

In case if you don't want items but only want to check condition, use LINQ Any method:如果您不想要物品但只想检查状况,请使用LINQ Any方法:

foreach (var stuff in listOfStuff)
{
    if (stuff.Constants.Any((keyValuePair) => keyValuePair.Key == "ConstantA"))
    {
        {
            //**I’m having problem with LINQ here**
        }
    }
}

In case if your Stuff class is defined using Dictionary :如果您的Stuff class 是使用Dictionary定义的:

public class Stuff
{
    public Dictionary<string, string> Constants { get; set; }
}

and usage:和用法:

var listOfStuff = CalculateStuff();
foreach (var stuff in listOfStuff)
{
    var items = stuff.Constants.Where((kvp) => kvp.Key == "ConstantA");

    if (items.Any())
    {
        //**I’m having problem with LINQ here**
    }
}

Note that with both cases usage is same, which means changing List<KeyValuePair<string, string>> to Dictionary<string, string> will not affect much code.请注意,两种情况的用法相同,这意味着将List<KeyValuePair<string, string>>更改为Dictionary<string, string>不会影响太多代码。


And finally a version I prefer the most )最后是我最喜欢的版本)
The Stuff class would be: Stuff class 将是:

public class Stuff
{
    public string StuffName { get; set; }
    public int StuffValue { get; set; }
}

Next, the calculate method would be:接下来,计算方法将是:

private static List<Stuff> CalculateStuff()
{
    return new List<Stuff>()
    {
        new Stuff{StuffName = "ConstantA", StuffValue = 100},
        new Stuff{StuffName = "ConstantB",StuffValue = 200}

    };
}

And the usage:以及用法:

var listOfStuff = CalculateStuff().Where(st => 
                                         st.StuffName == "ConstantA");

foreach (var stuff in listOfStuff)
{
    Console.WriteLine($"Name: {stuff.StuffName}, Value: {stuff.StuffValue}");
}

If you're trying to just check for at least one instance of ConstantA then just use 'Any()' like this:如果您只想检查ConstantA 的至少一个实例,那么只需像这样使用'Any()':

if (stuff.Constants.Any(x => x.Key == "ConstantA")
{
    //do something....
}

Concerning the problem raised, I propose two different approaches both of them using Linq hoping that you will be useful.关于提出的问题,我提出了两种不同的方法,它们都使用Linq希望对你有用。

1 - First Approache: 1 - 第一种方法:

var result = listOfStuff.SelectMany(e => e.Constants.Select(d => d))
                        .Where(e=> e.Key == "ConstantA");

2 - Second Approache: 2 - 第二种方法:

 var result = from item in listOfStuff.SelectMany(e => e.Constants)
             where item.Key =="ConstantA"
             select item ;

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

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