Select Multiple Fields from List in Linq
In ASP.NET C# I have a struct:
public struct Data
{
public int item1;
public int item2;
public int category_id;
public string category_name;
}
and I have a List of those. I want to select category_id
and category_name
, running a DISTINCT
and finally an ORDERBY
on category_name
.
Here's what I have now:
List<Data> listObject = getData();
string[] catNames = listObject
.Select(i=> i.category_name)
.Distinct()
.OrderByDescending(s => s)
.ToArray();
This obviously just gets the category name. My question is, how do I get multiple fields, and what data structure will I store this in (not a string[]
)?
EDIT
Using a list of structs is not set in stone. If it would be advisable to change my backing data structure to make selects easier (I'll be writing a lot of these) then I'd gladly take recommendations.
Solution 1:
Anonymous types allow you to select arbitrary fields into data structures that are strongly typed later on in your code:
var cats = listObject
.Select(i => new { i.category_id, i.category_name })
.Distinct()
.OrderByDescending(i => i.category_name)
.ToArray();
Since you (apparently) need to store it for later use, you could use the GroupBy operator:
Data[] cats = listObject
.GroupBy(i => new { i.category_id, i.category_name })
.OrderByDescending(g => g.Key.category_name)
.Select(g => g.First())
.ToArray();
Solution 2:
var selectedCategories =
from value in
(from data in listObject
orderby data.category_name descending
select new { ID = data.category_id, Name = data.category_name })
group value by value.Name into g
select g.First();
foreach (var category in selectedCategories) Console.WriteLine(category);
Edit: Made it more LINQ-ey!
Solution 3:
You could use an anonymous type:
.Select(i => new { i.name, i.category_name })
The compiler will generate the code for a class with name
and category_name
properties and returns instances of that class. You can also manually specify property names:
i => new { Id = i.category_id, Name = i.category_name }
You can have arbitrary number of properties.