c# - Linq query with grouping to fetch youngest date -
i designed query , added grouping grouping not reduce records youngest contractenddate. appears have no effect on result.
public iqueryable<soonobsoletecontractsviewmodel> get() { var countries = unitofwork.getall<country>(); var companies = unitofwork.getall<company>(); var contracts = unitofwork.getall<contract>(); var query = country in countries join company in companies on country.id equals company.countryid join contract in contracts on company.id equals contract.companyid select new soonobsoletecontractsviewmodel { countryname = contry.name, companyname = company.name, companyid = company.companyid contractbegindate = contract.begin, contractenddate = contract.end, contractid = contract.id }; query.groupby(c => c.companyid, (key, e) => e.orderby(f => f.contractenddate).first()); return query; }
any appreciated.
group country , company record in group recent contract end date.
var result = query .groupby(_ => new { _.countryname, _.companyid}) .select(g => g.orderbydescending(c => c.contractenddate).first()) .tolist();
or put way
var query = contry in countries join company in companies on country.id equals company.countryid join contract in contracts on company.id equals contract.companyid let viewmodel = new soonobsoletecontractsviewmodel { countryname = contry.name, companyname = company.name, companyid = company.companyid contractbegindate = contract.begin, contractenddate = contract.end, contractid = contract.id } group viewmodel new { viewmodel.countryname, viewmodel.companyid } g select g.orderbydescending(c => c.contractenddate).first();
wiki
Comments
Post a Comment