LINQ具有多个选择和多个选择组

时间:2017-04-04 14:27:24

标签: c# linq

我有一张这样的桌子:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "showCountries" {
            let data = sender as! [String: String]
            let destinationVC = segue.destination as! UserSelectedCountryTVC
            destinationVC.countryName           =   data["countryName"]!
            destinationVC.countryId             =   data["countryId"]!
            destinationVC.countryDialingCode    =   data["countryDialingCode"]!
    }

    if segue.identifier == "showSelectedCountry"{
            let data = sender as! [String: String]
            let destinationVC = segue.destination as! UserSelectedCountryTVC
            destinationVC.countryName           =   data["countryName"]!
            destinationVC.countryId             =   data["countryId"]!
            destinationVC.countryDialingCode    =   data["countryDialingCode"]!

        }

}

我想通过第一个和第二个字段对此表进行分组,但我想只获取具有最大第三个字段的行。

我如何通过c#中的LINQ执行此操作?

First Field | Second Field |  Third Field   |   Fourth Field
------------+--------------+----------------+----------------
 5      |     aaaa     |      47            |       abc
 5      |     aaaa     |      57            |       def
 5      |     aaaa     |      63            |       xyz
 5      |     bbbb     |      39            |       www
 5      |     bbbb     |      37            |       lol
 5      |     cccc     |      84            |       yuy

这是我的预期结果:

var curi = (from row in customClassList.AsEnumerable()
                    group row by new
                    {
                        First = row.FirstField.ToString(),
                        Second = row.SecondSield.ToString(),
                    } into grp
                    select new FieldsClass
                    {
                        First = grp.Key.First,
                        Second = grp.Key.Second,
                        Third = grp.Select(p => p.ThirdField).Max().ToShort(),
                        Fourth = grp.Key.Fourth
                    }).ToList();

1 个答案:

答案 0 :(得分:8)

怎么样

var orderQty = this.value;