我以list
的形式获得输出,如图所示。
此处month
是一个字符串,year
是一个整数。
答案 0 :(得分:1)
我假设左图中的表是input
表:
var result = from i in input
groupby new { i.Company, i.Year, i.Month } into g
select new {
g.Key.Company,
g.Key.Year,
g.Key.Month,
Version = g.Select(x => x.Version).Last()
};
答案 1 :(得分:1)
假设您的表名是 CompanyInfo ,那么
SQL Qyery:
select
Company,
Year,
Month,
Max(ActiveVersion) as ActiveVersion
from
CompanyInfo
Group by
Company,
Year,
Month
LINQ查询:
var result = from comp in CompanyInfo
group comp by new { comp.Company, comp.Year, comp.Month } into grp
select new {
grp.Key.Company,
grp.Key.Year,
grp.Key.Month,
Version = grp.Max(a => a.ActiveVersion)};
使用Lambda Expression:
var result = infos.GroupBy(grp => new { grp.Company, grp.Year, grp.Month })
.Select(t => new CompanyInfo()
{
Company = t.Key.Company,
Year = t.Key.Year,
Month = t.Key.Month,
ActiveVersion = t.Max(v => v.ActiveVersion)
}).ToList();
答案 2 :(得分:0)
List<Record> list = new List<Record>();
list.Add(new Record("ABC", 2014, "Jan", 1));
list.Add(new Record("ABC", 2014, "Jan", 2));
list.Add(new Record("ABC", 2014, "Jan", 3));
list.Add(new Record("ABC", 2014, "Feb", 1));
list.Add(new Record("ABC", 2014, "Feb", 2));
list.Add(new Record("ABC", 2014, "Mar", 1));
list.Add(new Record("ABC", 2014, "Mar", 2));
list.Add(new Record("ABC", 2014, "Mar", 3));
list.Add(new Record("ABC", 2014, "Apr", 1));
list.Add(new Record("ABC", 2015, "Jan", 1));
list.Add(new Record("ABC", 2015, "Jan", 2));
list.Add(new Record("ABC", 2015, "Feb", 1));
list.Add(new Record("ABC", 2015, "Mar", 1));
list.Add(new Record("ABC", 2015, "Apr", 1));
var result = from record in list
group record by new { record.Company, record.Year, record.Month } into g
select new {
g.Key.Company,
g.Key.Year,
g.Key.Month,
Version = g.Count()};