简体   繁体   中英

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. 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. I'm unfamiliar with how to get distinct values from a LINQ query. Any suggestions?

At this point, your 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

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.

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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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