我在LinqPad中试过这个
我有以下数据和linq查询,它只返回1条记录和2张图像,但返回2条记录和4张图像。
我知道我有什么不对,但不知道是什么。我在寻找用户的详细信息+汽车详情+图片来自用户:0b3c2ba5-1538-4557-a6c0-7de701fd83e7
它与出价有关,因为当我删除其中一个用户出价(0b3c2ba5-1538-4557-a6c0-7de701fd83e7)时,我得到1条记录和2张图片
TABLES
------
id cardid bidamount dateplaced usedid
43 83 625 2012-11-05 16:12:51.600 a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc
44 86 575 2012-11-05 16:15:02.257 a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc
45 83 650 2012-11-05 16:15:07.283 a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc
46 86 600 2012-11-05 17:45:04.140 a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc
47 86 625 2012-11-05 17:45:08.867 a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc
49 83 750 2012-11-07 13:40:37.590 0b3c2ba5-1538-4557-a6c0-7de701fd83e7
52 83 850 2012-11-08 13:40:37.590 0b3c2ba5-1538-4557-a6c0-7de701fd83e7
userid city state
0b3c2ba5-1538-4557-a6c0-7de701fd83e7 Sydney NSW
id name descr listingOption priceStarting priceReserve
83 Valiant Old Car 2 1000 1500
86 Volvo Safe Car 3 3000 4500
id image belongs_to
71 images/a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc/alpaca.JPG 83
72 images/a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc/a_bag.jpg 83
75 images/a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc/alpaca.JPG 86
76 images/a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc/a_bag.jpg 86
QUERY -----
var query = (from c in cars
from ud in users_details
from bd in bids
orderby c.listingOption descending
where a.userID == ud.userid
&& c.id == bd.carID
&& c.enabled == true
&& bd.userID == new Guid("0b3c2ba5-1538-4557-a6c0-7de701fd83e7")
let images = from ai in auction_images
where ai.belongs_to == c.id
select ai
let bid = (from b in bids
orderby b.id descending
where b.carID == c.id
select b.bidamount).FirstOrDefault()
select
new
{
images,
bidamount = (bid != null ? bid : 0),
ud.city,
ud.state,
c.name,
c.descr,
c.id,
c.listingOption,
c.priceStarting,
c.priceReserve,
bd.userID
}
);
query.Distinct().Dump();
答案 0 :(得分:0)
我正在寻找用户详细信息+汽车详情+图片来自用户: 0b3c2ba5-1538-4557-a6c0-7de701fd83e7
根据您在该行中所说的内容,您可以通过将查询拆分为3个单独的并使用Distinct函数来获取汽车ID来实现您的目标。
行动方针如下:
代码将是这样的:
var carIDs = bids.Where(x => x.usedid == new Guid(“0b3c2ba5-1538-4557-a6c0-7de701fd83e7”))。选择(x => x.cardid).Distinct() ;
然后您可以选择用户详细信息和图像(在两个单独的查询中)。
答案 1 :(得分:0)
这是一项非常大的家庭作业。你去吧,
使用此查询
var query = from bd in lstbids
group bd by new { carid = bd.carid, userid = bd.userid } into gbd
join ud in lstuserdetails on gbd.First().userid equals ud.userid
join c in lstcars on gbd.First().carid equals c.id
where c.enabled && ud.userid == new Guid("0b3c2ba5-1538-4557-a6c0-7de701fd83e7")
select new
{
images = lstaction_images.Where(ai => ai.belongs_to == c.id),
bidamount = lstbids.Where(b => b.carid == c.id).OrderByDescending(b => b.id).Select(b => b.bidamount).DefaultIfEmpty(0).FirstOrDefault(),
ud.city,
ud.state,
c.name,
c.descr,
c.id,
c.listingOption,
c.priceStarting,
c.priceReserve,
gbd.FirstOrDefault().userid
};
注意:可以更好地编写此查询。
以下是示例表格(我认为您的表格为我的列表):
表格/类
public class userdetails
{
public Guid userid { get; set; }
public string city { get; set; }
public string state { get; set; }
}
public class bids
{
public int id { get; set; }
public int carid { get; set; }
public int bidamount { get; set; }
public DateTime dateplaced { get; set; }
public Guid userid { get; set; }
}
public class cars
{
public int id { get; set; }
public string name { get; set; }
public string descr { get; set; }
public int listingOption { get; set; }
public int priceStarting { get; set; }
public int priceReserve { get; set; }
public bool enabled { get; set; }
}
public class action_images
{
public int id { get; set; }
public string image { get; set; }
public int belongs_to { get; set; }
}
数据/记录
List<bids> lstbids = new List<bids>
{
new bids {id= 43, carid=83, bidamount=625, dateplaced=Convert.ToDateTime("2012-11-05 16:12:51.600"), userid=new Guid("a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc") },
new bids {id= 44, carid=86, bidamount=575, dateplaced=Convert.ToDateTime("2012-11-05 16:15:02.257"), userid=new Guid("a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc") },
new bids {id= 45, carid=83, bidamount=650, dateplaced=Convert.ToDateTime("2012-11-05 16:15:07.283"), userid=new Guid("a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc") },
new bids {id= 46, carid=86, bidamount=600, dateplaced=Convert.ToDateTime("2012-11-05 17:45:04.140"), userid=new Guid("a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc") },
new bids {id= 47, carid=86, bidamount=625, dateplaced=Convert.ToDateTime("2012-11-05 17:45:08.867"), userid=new Guid("a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc") },
new bids {id= 49, carid=83, bidamount=750, dateplaced=Convert.ToDateTime("2012-11-07 13:40:37.590"), userid=new Guid("0b3c2ba5-1538-4557-a6c0-7de701fd83e7") },
new bids {id= 52, carid=83, bidamount=850, dateplaced=Convert.ToDateTime("2012-11-08 13:40:37.590"), userid=new Guid("0b3c2ba5-1538-4557-a6c0-7de701fd83e7") },
};
List<userdetails> lstuserdetails = new List<userdetails>
{
new userdetails { userid = new Guid ("0b3c2ba5-1538-4557-a6c0-7de701fd83e7"), city = "Sydney", state = "NSW"},
};
List<cars> lstcars = new List<cars>
{
new cars {id = 83, name = "Valiant", descr = "Old Car", listingOption = 2, priceStarting = 1000, priceReserve = 1500, enabled = true },
new cars {id = 86, name = "Volvo", descr = "Safe Car", listingOption = 3, priceStarting = 3000, priceReserve = 4500, enabled = true },
};
List<action_images> lstaction_images = new List<action_images>
{
new action_images { id = 71 , image = "images/a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc/alpaca.JPG", belongs_to =83 },
new action_images { id = 72 , image = "images/a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc/a_bag.jpg", belongs_to =83 },
new action_images { id = 75 , image = "images/a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc/alpaca.JPG", belongs_to =86 },
new action_images { id = 76 , image = "images/a5d383e0-0c2c-44cf-9da1-0ce364b1dbdc/a_bag.jpg", belongs_to =86 },
};