【问题标题】:Search in the list and get all related data c#在列表中搜索并获取所有相关数据 c#
【发布时间】:2018-05-27 12:08:36
【问题描述】:

我有列表,我需要搜索特定项目并从该列表中获取相关数据。

 public People(string name, string talent)
        {
            Name= name;
            Talent= talent;
        }
        public string Name;
        public int Talent;

我在该列表中添加了许多项目

List<People> peopleList = new List<People>();
For example;
"John","magic",
"John","windpower",
"John","electricpower",
"John","firepower",
"Luigi","storm",
"Luigi","fire",

我正在努力让所有人才都喜欢​​;

string talentsofJohn = "magic,windpower,electricpower,firepower";
string talentsofLuigi = "storm,fire";

我尝试了很多方法,尤其是 Linq,但无法做到;

var talentsofJohn = peopleList.Find(item => item.name == "John");

【问题讨论】:

  • People 是复数;你的班级最好命名为“人”
  • CaiusJard 对不起我的英语这是我最好的:) @ThrowingSpoon 是的名字是独一无二的

标签: c# arrays list oop


【解决方案1】:

你快到了,你只是没有在“收集项目”模式下思考

var talentsofJohn = peopleList.Find(item => item.name == "John");

只会得到一个 People 对象,因为一旦找到第一个匹配项,Find 就会停止

您想获得所有匹配项的集合,并且您想通过 item.Name == "John" 查找 - 请注意,我已将您的 name 的大写更改为 Name,因为这是公开可见的财产的名称。

为此,您可以使用 LINQ .Where - 此 Where 的结果是 People 对象的可枚举集合。从此,您想对每个人的 Talent 属性进行 LINQ Select (选择将为您提供可枚举的人才字符串集合):

var talentsofJohn = peopleList.Where(p => p.Name == "John").Select(q => q.Talent);

如果你想把它们全部放在一个字符串中,用逗号分隔:

var str = string.Join(",", talentsOfJohn);

注意,您可以避免使用 string.Join,并使用 Aggregate 方法在 LINQ 中完成所有这些操作,但您可能不希望这样做有几个很好的理由。欲了解更多信息,包括关于原因的论点,请参阅Concat all strings inside a List<string> using LINQ

最后一点.. List.Find 不是 LINQ.. 它只是 List 上的一个普通旧方法。因此,如果您确实将此 LINQ 代码放入您的应用程序中,它会显示如下内容:

列表不包含“Where”的定义,并且找不到接受 blahblah 的第一个参数的扩展方法,您是否缺少程序集引用?

您需要确保您的类文件位于顶部:

using System.Linq;

【讨论】:

  • 你是我的雷神,你是幸福的创造者。
【解决方案2】:

您的Talent 类型为int,请将其更改为string,否则将无法正常工作。

然后试试这个来为特定的人检索天赋:

string userName = "John";
var talentsOfPerson = peopleList
                        .Where(person => person.Name == userName)
                        .Select(person => person.Talent)
                        .ToList();

你的班级的正确名称应该是 Person 而不是 People

【讨论】:

  • 你也是我的雷神,你也是幸福的创造者:)
【解决方案3】:

首先,让我们使用 Where 将您的列表过滤到仅包含名为 John 的人。

var johnList = peopleList.Where(item => item.name == "John");

接下来,我们用Select来只提取人的天赋:

var talentList = johnList.Select(item => item.talent);

最后,您可以使用string.Join 将数组或列表转换为字符串:

string talents = string.Join(",", talentList);

LINQ 允许您将这些链接在一起,因此您可以在一行中完成所有操作:

string talents = string.Join(
    ",", 
    peopleList
        .Where(item => item.name == "John")
        .Select(item => item.talent)
    );

【讨论】:

  • 我有些怀疑你有什么权力。
【解决方案4】:

使用您选择的循环,例如 for 循环、for each loop、while 循环等

foreach(people p in peoplelist)
{ 
  if(p.name=="john")
   {

    Console.WriteLine(p.name + " " + p.talent)

   }
}

for(int i=0;i<peoplelist.Count;i++)
{
     if(peoplelist[i].name=="john")
       {

        Console.WriteLine(peoplelist[i].name + " " + peoplelist[i].talent)

       }
}

希望对你有帮助

【讨论】:

  • 你也是我的雷神,你也是幸福的创造者:)
【解决方案5】:

您需要结合使用Where(过滤列表)和Select(选择单个属性):

var talents = peopleList.Where(p => p.Name == "John").Select(p => p.Talent);

如果您希望它以逗号分隔的字符串形式出现,您可以在返回的枚举上使用 string.Join

var asString = string.Join(",", talents)

【讨论】:

  • 绝对宙斯或火创造者。我只是不知道
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
相关资源
最近更新 更多