代码之家  ›  专栏  ›  技术社区  ›  Byron Sommardahl

什么是linq查询,它将在objx.created=最新处获取每个objx?

  •  1
  • Byron Sommardahl  · 技术社区  · 16 年前

    这个对我来说是个脑筋急转弯。我几乎不想寻求帮助,因为我担心自己会错过这个无休止的、不眠的夜晚,试图破译这个谜。JK

    我有一个C项目,我需要在其中显示一个唯一对象的列表,但只有基于对象类型的最新对象。为了讨论的目的,我们来谈谈“水果”。假设我有一份水果清单,每个都有一个“采摘日期”。没有主键。所以,我的“水果”类的一般列表可能看起来像…

    {'Apple','1/2/2010'}
    {'Apple','11/12/2009'}
    {'Apple','2/14/2010'}
    {'Grape','5/2/2009'}
    {'Orange','10/30/2009'}
    {'Mango','2/13/2010'}
    {'Apple','6/30/2009'}
    {'Orange','10/5/2009'}
    {'Grape','2/1/2010'}
    

    我需要能够把这个单子缩减到每种水果中最新的一种。结果应该是…

    {'Apple','2/14/2010'}
    {'Orange','10/30/2009'}
    {'Mango','2/13/2010'}
    {'Grape','2/1/2010'}
    

    在我的实际情况中,我使用的是Linq to SQL。所以,我想和我一直在做的事情保持一致。

    这可能是一件很简单的事情,后来我甚至会被问到。但是,我需要知道,所以我想我必须做出牺牲。

    6 回复  |  直到 16 年前
        1
  •  6
  •   Jon Skeet    16 年前

    像这样的事情应该可以做到:

    var query = from item in db.Items
                group item by item.Fruit into grouped
                select grouped.OrderByDescending(x => x.Date)
                              .First();
    

    换句话说,按水果名称分组,以及 每组 按日期排序并获取第一个结果。

        2
  •  1
  •   casperOne    16 年前

    您希望对键(在本例中是水果)执行分组,然后对值(在本例中是创建的)对组进行排序,从每个组中选择第一个项目,按顺序排列,如下所示:

    from each f in FruitCreatedDates
    group f by f.Created into g
    select new 
    { 
        Fruit = g.Key, 
        Newest = g.OrderedByDescending(fr => fr.Created).First() 
    };
    
        3
  •  0
  •   Pbirkoff    16 年前

    我不太熟悉linq-to-sql语法,但是在SQL中,您将使用不同的select,并结合创建日期的顺序。

        4
  •  0
  •   Antony Scott    16 年前

    看起来你需要一个Groupby和一个Max(日期)。我只是在用SQL的方式思考,但我相信您能够为它找到LINQ。

        5
  •  0
  •   recursive    16 年前

    使用 Min 方法,提供这样的选择器lambda:

    var list = new List<Fruit>() { 
        new Fruit() {Name = "Apple", Created = new DateTime(2010, 1, 2)},
        new Fruit() {Name = "Apple", Created = new DateTime(2010, 2, 2)},
        new Fruit() {Name = "Apple", Created = new DateTime(2010, 3, 2)},
        new Fruit() {Name = "Grape", Created = new DateTime(2011, 4, 2)},
        new Fruit() {Name = "Grape", Created = new DateTime(2011, 5, 2)},
        new Fruit() {Name = "Grape", Created = new DateTime(2011, 6, 2)},
    };
    
    var query =
            from fruit in list
            group fruit by fruit.Name into grouped
            select grouped.Min(f => f.Created);
    
        6
  •  0
  •   sidney.andrews    16 年前

    如果我是你,我会创建一个自定义对象来封装这些数据。通过这种方式,您可以运行LINQ查询以满足您的心愿。

    public struct Fruit
    {
        public string Name;
        public string FreshDate;
    }
    

    然后我会这样运行查询:

    List<Fruit> fruitArray = new List<Fruit>
    {
    new Fruit { Name = "Apple", FreshDate = "1/2/2010"},
    new Fruit { Name = "Apple", FreshDate = "11/12/2009"},
    new Fruit { Name = "Apple", FreshDate = "2/14/2010"},
    new Fruit { Name = "Grape", FreshDate = "5/2/2009"},
    new Fruit { Name = "Orange", FreshDate = "10/30/2009"},
    new Fruit { Name = "Mango", FreshDate = "2/13/2010"},
    new Fruit { Name = "Apple", FreshDate = "6/30/2009"},
    new Fruit { Name = "Orange", FreshDate = "10/5/2009"},
    new Fruit { Name = "Grape", FreshDate = "2/1/2010"}
    };
    
    var resultArray = fruitArray.GroupBy(f => f.Name).Select(g => g.OrderBy(f => DateTime.Parse(f.FreshDate)).Last());
    

    您不应该在多维字符串数组上运行查询。在自定义对象上更容易阅读。