linq to sql 中的分组依据和连接表

发布于 2024-11-26 12:03:45 字数 358 浏览 1 评论 0原文

我有以下 3 个类(映射到 sql 表)。

Places table:
Name(key)
Address
Capacity

Events table:
Name(key)
Date
Place

Orders table:
Id(key)
EventName
Qty

Places 和 Events 表通过 Places.Name = Events.Place 连接,而 Events 和 Orders 表: Events.Name = Orders.EventName 。 任务是给定一个事件,归还该事件剩余的门票。容量是一个地方可以容纳的数量,数量是某人订购的门票数量。因此需要在订单表中进行某种分组,然后从容量中减去总和。

I have the following 3 classes(mapped to sql tables).

Places table:
Name(key)
Address
Capacity

Events table:
Name(key)
Date
Place

Orders table:
Id(key)
EventName
Qty

The Places and Events tables are connected through Places.Name = Events.Place, while the Events and Orders tables: Events.Name = Orders.EventName .
The task is that given an event, return the tickets left for that event. Capacity is the number a place can hold and Qty is the number of tickets ordered by someone. So some sort of grouping in the Orders table is needed and then subtract the sum from capacity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何处潇湘 2024-12-03 12:03:45

像这样的东西(下面的 C# 代码示例)?

抱歉,变量名称很奇怪,但事件是一个关键字:)
我没有使用Visual Studio,所以我希望语法是正确的。

string eventName = "Event";

var theEvent = Events.FirstOrDefault(ev => ev.Name == eventName);
int eventOrderNo = Orders.Count(or => or.EventName == eventName);

var thePlace = Places.FirstOrDefault(pl => pl.Name == theEvent.Place);
int ticketsLeft = thePlace.Capacity - eventOrderNo;

如果事件有多个地点,最后两行将如下所示:

int placesCapacity = Places.Where(pl => pl.Name == theEvent.Place)
                           .Sum(pl => pl.Capacity);

int ticketsLeft = placesCapacity - eventOrderNo;

旁注
LINQ 101 是熟悉 LINQ 的好方法:http://msdn.microsoft.com /zh-cn/vcsharp/aa336746

Something like this (C# code sample below)?

Sorry for the weird variable names, but event is a keyword :)
I didn't use visual studio, so I hope that the syntax is correct.

string eventName = "Event";

var theEvent = Events.FirstOrDefault(ev => ev.Name == eventName);
int eventOrderNo = Orders.Count(or => or.EventName == eventName);

var thePlace = Places.FirstOrDefault(pl => pl.Name == theEvent.Place);
int ticketsLeft = thePlace.Capacity - eventOrderNo;

If the Event has multiple places, the last two lines would look like this:

int placesCapacity = Places.Where(pl => pl.Name == theEvent.Place)
                           .Sum(pl => pl.Capacity);

int ticketsLeft = placesCapacity - eventOrderNo;

On a sidenote
LINQ 101 is a great way to get familiar with LINQ: http://msdn.microsoft.com/en-us/vcsharp/aa336746

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文