Linq to SQL - 处理另一个对象内的对象集合的最佳方式

发布于 2024-12-07 21:38:54 字数 926 浏览 0 评论 0原文

我有一个用于拍卖的物品表和一个针对这些物品的出价表。数据库还有很多内容,但我们会保持简单。

public class Items
{
    public int ItemID { get; set; }
    public string ItemName { get; set; }
    public List<Bid> Bids { get; set; }
}

public class Bids
{
    public int BidID { get; set; }
    public int ItemID  { get; set; }
    public decimal Amount { get; set; }
    public datetime BidTime { get; set; }
    public int CustomerID { get; set; }
}

我想返回一个数据集,其中包括 ItemID、ItemName 以及按 BidTime 降序排列的所有关联出价记录。最后,我只想查看某个客户已出价的商品,并且我只想查看他们对该商品的出价。 Bis.ItemID 和 Items.ItemID 之间存在外键关系。我正在使用 Linq to SQL。

这有效并且似乎返回了正确的数据集:

from i in Items
from b in i.Bids
where i.AuctionID == 2 && b.CustomerID == (Int32?)1165
orderby b.BidTime descending
select new
{
    i.ItemID,
    i.ItemName,
    i.Bids
}

我是一个 SQL 专家,试图理解 Linq 的 OO 本质。这是获得我想要的结果的最佳方法(甚至是好方法)吗?有更好的办法吗?

谢谢,

BK

I have a table of Items for auction and a table of bids made for those items. There's much more to the database but we'll keep it simple.

public class Items
{
    public int ItemID { get; set; }
    public string ItemName { get; set; }
    public List<Bid> Bids { get; set; }
}

public class Bids
{
    public int BidID { get; set; }
    public int ItemID  { get; set; }
    public decimal Amount { get; set; }
    public datetime BidTime { get; set; }
    public int CustomerID { get; set; }
}

I want to return a dataset that includes the ItemID, the ItemName and all of the associated bid records ordered by BidTime, descending. Finally, I'd like to only see Items that a certain Customer has bid on and I'd like to only see their bids for that item. There is a foreign key relationship between Bids.ItemID and Items.ItemID. I'm using Linq to SQL.

This works and appears to return the correct dataset:

from i in Items
from b in i.Bids
where i.AuctionID == 2 && b.CustomerID == (Int32?)1165
orderby b.BidTime descending
select new
{
    i.ItemID,
    i.ItemName,
    i.Bids
}

I'm a SQL guy trying to wrap my head around the OO nature of Linq. Is this the best way (or even a good way) to get the results I want? Is there a better way?

Thanks,

BK

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

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

发布评论

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

评论(1

温折酒 2024-12-14 21:38:54

这个怎么样(稍微简单一点的语法):

由于注释而更新了 where 子句

from i in db.items
where i.AuctionID == 2 && i.Bids.Any(c=>c.CustomerID == (Int32?)1165)
select new
{ i.ItemId,
  i.ItemName,
  i.Bids.Where(b=>b.CustomerID == (Int32?)1165 ).OrderbyDescending(b=>b.BidTime)
 }

确保您分析了 sql 并验证您只获得了一个 sql 语句。如果没有,您需要将 LoadOptions 添加到您的数据上下文中(这将确保所有出价都急切地加载您的项目)。

这也会对每件商品的出价进行排序(不确定您的是否会做同样的事情......但我猜您检查过)。

How about this one (slightly more simple syntax):

Updated where clause due to comment

from i in db.items
where i.AuctionID == 2 && i.Bids.Any(c=>c.CustomerID == (Int32?)1165)
select new
{ i.ItemId,
  i.ItemName,
  i.Bids.Where(b=>b.CustomerID == (Int32?)1165 ).OrderbyDescending(b=>b.BidTime)
 }

Make sure you profile the sql and verify that you get only ONE sql statement. If not, you need to have to add LoadOptions to your datacontext (that will make sure all the bids are eager loaded with your items).

This one will also order the bids per item (not sure if yours will do the same.... but I guess you checked that).

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