Csharp/C#教程:Linq to SQL with Group by分享


Linq to SQL with Group by

我试图将此T-SQL转换为Linq To SQL,但无法通过聚合函数计算出组。 欢迎任何帮助。

select c.ClientID, GivenName, Surname, max(a.Address), max(t.Value) from Client c left join ClientAddress a on c.ClientID = a.ClientID left join ClientContact t on c.ClientID = t.ClientID group by c.ClientID, GivenName, Surname 

要按组合键进行分组,通常使用匿名类型:

  var qry = from x in someSource group x by new { x.ClientID, x.GivenName, x.Surname } into grp select new { grp.Key, Address = grp.Max(x => x.Address), Value = grp.Max(x => x.Value) }; 

我想出的确切答案是

上述就是C#学习教程:Linq to SQL with Group by分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 public IQueryable GetClientsDTO() { return (from client in this.Context.Clients join address in this.Context.ClientAddresses on client.ClientID equals address.ClientID join contact in this.Context.ClientContacts on client.ClientID equals contact.ClientID where contact.ContactType == "Phone" group client by new { client.ClientID, client.Surname, client.GivenName } into clientGroup select new ClientSearchDTO() { ClientID = clientGroup.Key.ClientID, Surname = clientGroup.Key.Surname, GivenName = clientGroup.Key.GivenName, Address = clientGroup.Max(x => x.ClientAddresses.FirstOrDefault().Address), PhoneNumber = clientGroup.Max(x => x.ClientContacts.FirstOrDefault().Value) }); } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1002166.html

(0)
上一篇 2021年12月28日
下一篇 2021年12月28日

精彩推荐