简体   繁体   English

如何将LINQ查询中的不同值放入列表中?

[英]How do I put distinct values from a LINQ query into a list?

I've looked at several answers to similar questions, but none of them solve my problem. 我已经看过类似问题的几个答案,但是没有一个能解决我的问题。 All I need to do is get distinct values from a LINQ query (that queries a XML file) and put them into a list. 我需要做的就是从LINQ查询(查询XML文件)中获得不同的值,并将它们放入列表中。 Here is what I have tried: 这是我尝试过的:

 var XmlData = XDocument.Load("PathToFile");
    List<string> XmlItems = new List<string>();

    var XQuery = from m in XmlData.Root.Elements()
                 where m.Attribute("Category").Value.ToString().Equals("TheCategory")
                 select (m.Attribute("TheAttribute").Value).Distinct().ToString();

    XmlItems.AddRange(XQuery);

    foreach (var item in XmlItems)
    {
        ComboBoxTeams.Items.Add(item);
    }

The Distinct() function call is not giving the expected result. Distinct()函数调用未提供预期的结果。 I'm unfamiliar with how to get distinct values from a LINQ query. 我不熟悉如何从LINQ查询中获取不同的值。 Any suggestions? 有什么建议么?

At this point, your Distinct 在这一点上,您Distinct

var XQuery = from m in XmlData.Root.Elements()
             where m.Attribute("Category").Value.ToString().Equals("TheCategory")
             select (m.Attribute("TheAttribute").Value).Distinct().ToString();

is only for (m.Attribute("TheAttribute").Value) , not for the whole statement 仅适用于(m.Attribute("TheAttribute").Value) ,不适用于整个语句

You may need to change it to 您可能需要将其更改为

var XQuery = from m in XmlData.Root.Elements()
             where m.Attribute("Category").Value.ToString().Equals("TheCategory")
             select (m.Attribute("TheAttribute").Value.ToString()); //get everything first, ToString probably needed
var XQueryDistinct = XQuery.Distinct(); //get distinct among everything you got

You have the .ToString() and .Distinct() in the wrong places. 您在错误的位置放置了.ToString()和.Distinct()。

var XmlData = XDocument.Load("PathToFile");
List<string> XmlItems = new List<string>();

var XQuery = from m in XmlData.Root.Elements()
             where m.Attribute("Category").Value.ToString().Equals("TheCategory")
             select (m.Attribute("TheAttribute").Value).Distinct().ToString();

XmlItems.AddRange(XQuery);

foreach (var item in XmlItems)
{
    ComboBoxTeams.Items.Add(item);
}

becomes: 变成:

var XmlData = XDocument.Load("PathToFile");
var XmlItems = (from m in XmlData.Root.Elements()
                 where m.Attribute("Category").Value.ToString().Equals("TheCategory")
                 select (m.Attribute("TheAttribute").Value.ToString())).Distinct();

foreach (var item in XmlItems)
{
    ComboBoxTeams.Items.Add(item);
}

If you have a list of simple values, you need to remove the use of Distinct in your select and put after. 如果您有一个简单值列表,则需要在选择和放置之后删除Distinct的使用。

var XQuery = (from m in XmlData.Root.Elements()
             where m.Attribute("Category").Value.ToString().Equals("TheCategory")
             select (m.Attribute("TheAttribute").Value.ToString())).Distinct();

If you have complex objects you have two alternatives: 如果您有复杂的对象,则有两种选择:

Using morelinq you can use DistinctBy: 使用morelinq可以使用DistinctBy:

XmlItems.DistinctBy(x => x.WhateverProperty);

Otherwise, you can use a group: 否则,您可以使用一个组:

XmlItems.GroupBy(x => x.idOrWhateverOtherProperty)
      .Select(g => g.First());

You might try 你可以试试

var uniqueList = yourList.Distinct().ToList();

after you got your non-unique list. 获得非唯一列表后。

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

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