首页 文章

从Linq中的列表中选择多个字段

提问于
浏览
111

在ASP.NET C#中我有一个结构:

public struct Data
{
    public int item1;
    public int item2;
    public int category_id;
    public string category_name;
}

我有一份清单 . 我想选择 category_idcategory_name ,运行 DISTINCT ,最后选择 ORDERBY category_name .

这就是我现在拥有的:

List<Data> listObject = getData();
string[] catNames = listObject
                    .Select(i=> i.category_name)
                    .Distinct()
                    .OrderByDescending(s => s)
                    .ToArray();

这显然只是得到了类别名称 . 我的问题是,如何获得多个字段,以及将其存储在哪个数据结构中(而不是 string[] )?

编辑

使用结构列表不是一成不变的 . 如果建议更改我的支持数据结构以使选择更容易(我将写很多这些),那么我很乐意接受建议 .

9 回答

  • 197
    (from i in list
     select new { i.category_id, i.category_name })
     .Distinct()
     .OrderBy(i => i.category_name);
    
  • 23
    var result = listObject.Select( i => new{ i.category_name, i.category_id } )
    

    这使用匿名类型,因此您必须使用var关键字,因为事先不知道表达式的结果类型 .

  • 3

    你可以把它变成KeyValuePair,所以它会返回一个 "IEnumerable<KeyValuePair<string, string>>"

    所以,它会是这样的:

    .Select(i => new KeyValuePair<string, string>(i.category_id, i.category_name )).Distinct();
    
  • 4
    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 :让它更加LINQ-ey!

  • 2

    您可以使用匿名类型:

    .Select(i => new { i.name, i.category_name })
    

    编译器将为具有 namecategory_name 属性的类生成代码,并返回该类的实例 . 您也可以手动指定属性名称:

    i => new { Id = i.category_id, Name = i.category_name }
    

    您可以拥有任意数量的属性 .

  • 21

    这是anonymous types非常适合的任务 . 您可以返回由编译器自动创建的类型的对象,从使用情况推断 .

    语法是这种形式:

    new { Property1 = value1, Property2 = value2, ... }
    

    对于您的情况,请尝试以下内容:

    var listObject = getData();
    var catNames = listObject.Select(i =>
        new { CatName = i.category_name, Item1 = i.item1, Item2 = i.item2 })
        .Distinct().OrderByDescending(s => s).ToArray();
    
  • 0
    public class Student
    {
        public string Name { set; get; }
        public int ID { set; get; }
    }
    
    class Program
    {
      static void Main(string[] args)
        {
            Student[] students =
            {
            new Student { Name="zoyeb" , ID=1},
            new Student { Name="Siddiq" , ID=2},
            new Student { Name="sam" , ID=3},
            new Student { Name="james" , ID=4},
            new Student { Name="sonia" , ID=5}
            };
    
            var studentCollection = from s in students select new { s.ID , s.Name};
    
            foreach (var student in studentCollection)
            {
                Console.WriteLine(student.Name);
                Console.WriteLine(student.ID);
            }
        }
    }
    
  • 5

    匿名类型允许您在以后在代码中强类型的数据结构中选择任意字段:

    var cats = listObject
        .Select(i => new { i.category_id, i.category_name })
        .Distinct()
        .OrderByDescending(i => i.category_name)
        .ToArray();
    

    由于您(显然)需要存储它以供以后使用,因此您可以使用GroupBy运算符:

    Data[] cats = listObject
        .GroupBy(i => new { i.category_id, i.category_name })
        .OrderByDescending(g => g.Key.category_name)
        .Select(g => g.First())
        .ToArray();
    
  • 3

    您可以使用linq Select选择多个字段 . 如上所示,在各种示例中,它将作为匿名类型返回 . 如果你想避免这种匿名类型,这是一个简单的技巧 .

    var items = listObject.Select(f => new List<int>() { f.Item1, f.Item2 }).SelectMany(item => item).Distinct();
    

    我认为这解决了你的问题

相关问题