给定此类型的DB结构:
Auction =0..N=> Bidders =0..N=> Bids
其中每个实体都有多个字段(例如拍卖标题,bidder.name,bid.date,bid.amount等)。
并且给出了拍卖ID,我想运行一次性提取的LINQ查询:
{ AuctionId, AuctionTitle, AuctionStartDate, ..., IdOfTheBestBidder, NameOfTheBestBidder, IdOfTheBestBid, AmountOfTheBestBid }
所有这一切都是一次性和最有效的方式。即不加载所有投标人和/或所有投标以进行连续处理。
var qry = from auction in db.Auctions
from bidder in auction.Bidders
...;
答案 0 :(得分:3)
假设"最佳出价"是具有最高金额的出价,您可以构建LINQ to Entities查询,该查询按降序按数量排序并获取第一个(包含所有相关数据),然后仅投影所需的字段。它将作为单个SQL查询进行翻译和执行(不会在客户端内存中加载拍卖,出价工具或出价对象)。
对于单一拍卖,它会是这样的:
SIOCETHTOOL
对于所有拍卖,它会是相似的,但每次拍卖都有子查询:
var result = (
from auction in db.Auctions
where auction.Id == auctionId
from bidder in auction.Bidders
from bid in bidder.Bids
orderby bid.Amount descending
select new
{
AuctionId = auction.Id,
AuctionTitle = auction.Title,
AuctionStartDate = auction.StartDate,
...,
IdOfTheBestBidder = bidder.Id,
NameOfTheBestBidder = bidder.Name,
IdOfTheBestBid = bid.Id,
AmountOfTheBestBid = bid.Amount,
}).FirstOrDefault();
答案 1 :(得分:0)
您可以使用两个linq查询,第一个查找最佳出价如下,
var biddersList= ( from bidder in db.Bidders
join bid in db.Bids
on bidder.Id equals bid.bidderId
where //Here you can give the conditions to select best bid of a bidder
group new { bidder, bid } by new { bidder.AuctionId } into bidandbidder
from grp in bidandbidder.DefaultIfEmpty()
select new {
grp.bidder.AuctionId,
grp.bidder.Id,
grp.bidder.Name,
grp.bid.Id,
grp.bid.Amount
}
);
在第二个linq查询中,使用biddersList
按如下方式查找具有给定given_auction_Id
的最佳出价者最佳出价的竞价详细信息。
var auctionList = ( from ac in db.Auction
join bd in biddersList
on ac.Id equals bd.AuctionId
where ac.Id == given_auction_Id and //here you can give the conditions to select best bidder
select new{
// select fields
}
);