具有内连接、多个分组依据和最小值最大值的 Linq 查询
我正在尝试将以下查询转换为 linq。
SELECT Client.Nom, Client.Prenom AS Prénom, Client.Structure, MIN(Meeting.Start) AS Début, MAX(Meeting.Start) AS Fin, Meeting.Dispositifs AS Dispositif
FROM Meeting INNER JOIN
Client ON Meeting.CliID = Client.CliID
WHERE (Client.Nom LIKE 'kess%')
GROUP BY Client.Nom, Client.Prenom, Client.Structure, Meeting.Dispositifs
ORDER BY Début DESC, Fin
到目前为止我所拥有的是这样的:
var clients = this.ObjectContext.Clients;
var meetings = this.ObjectContext.Meetings;
//int i = 0;
var query = from personne in clients
join meeting in meetings on personne.CliID equals meeting.CliID
where personne.CliID == guid
group personne by new { personne.Nom, personne.Prenom, personne.Structure, meeting.Dispositifs } into g
select new Parcour
{
//ID = i++,
Nom = g.Key.Nom,
Prénom = g.Key.Prenom,
Structure = g.Key.Structure,
Début = g.Min(m => m.Start),
Fin = g.Max(m => m.Start),
Dispositif = g.Key.Dispositifs,
};
return query.ToList<Parcour>();
我的问题是访问 Start 属性,它是会议的属性。
另外,有没有一种方法可以让像 ID 这样的键包含一个自动递增的 int ?
任何帮助将不胜感激, 约翰.
I'm trying to convert the following query to linq.
SELECT Client.Nom, Client.Prenom AS Prénom, Client.Structure, MIN(Meeting.Start) AS Début, MAX(Meeting.Start) AS Fin, Meeting.Dispositifs AS Dispositif
FROM Meeting INNER JOIN
Client ON Meeting.CliID = Client.CliID
WHERE (Client.Nom LIKE 'kess%')
GROUP BY Client.Nom, Client.Prenom, Client.Structure, Meeting.Dispositifs
ORDER BY Début DESC, Fin
What I have so far is this:
var clients = this.ObjectContext.Clients;
var meetings = this.ObjectContext.Meetings;
//int i = 0;
var query = from personne in clients
join meeting in meetings on personne.CliID equals meeting.CliID
where personne.CliID == guid
group personne by new { personne.Nom, personne.Prenom, personne.Structure, meeting.Dispositifs } into g
select new Parcour
{
//ID = i++,
Nom = g.Key.Nom,
Prénom = g.Key.Prenom,
Structure = g.Key.Structure,
Début = g.Min(m => m.Start),
Fin = g.Max(m => m.Start),
Dispositif = g.Key.Dispositifs,
};
return query.ToList<Parcour>();
My problem is to get access to the Start property which is a property of meeting.
Also, is there a way to have a key like ID here that would contain an int that gets autoincremented?
Any help would be much appreciated,
John.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,让我们修复 LINQ。 几乎总是在 LINQ to Entities 中使用
join
是错误的。此外,您似乎仅使用 SQL
GROUP BY
来方便使用每个客户端的聚合MIN
和MAX
。 LINQ to Entities 不需要这个。所以我们也可以简化它。这给我们留下了。
然后你问:
目前尚不清楚你想要什么。
如果您需要数据库键,请使用 AUTOINCREMENT 列。 EF 支持这一点。
如果您想要仅此方法的递增数字,则使用对 Enumerable.Select 的索引重载。
First, let's fix the LINQ. It's nearly always wrong to use
join
in LINQ to Entities.Also, you seem to be using the SQL
GROUP BY
solely to facilitate use of the aggregateMIN
andMAX
per-client. LINQ to Entities doesn't require this. So we can simplify that, too.That leaves us with.
Then you ask:
It's not clear what you want here.
If you want a database key, then use an AUTOINCREMENT column. EF supports that.
If you want an incrementing number for this method only, then us the indexed overload to Enumerable.Select.