gpt4 book ai didi

c# - 复杂的 Linq C# 查询

转载 作者:行者123 更新时间:2023-11-30 18:36:57 24 4
gpt4 key购买 nike

我在 LinqPad 中试过了

我有以下数据和 linq 查询,它应该只返回 1 条记录和 2 张图像,但返回 2 条记录和 4 张图像。

我知道我有问题,但不知道是什么。我正在寻找用户详细信息 + 汽车详细信息 + 用户图像:0b3c2ba5-1538-4557-a6c0-7de701fd83e7

这与出价有关,因为当我删除他为用户 (0b3c2ba5-1538-4557-a6c0-7de701fd83e7) 出价之一时,我会得到 1 条记录和 2 张图片

表格
------

表格出价

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

TABLE 用户详细信息

userid                                  city    state  
0b3c2ba5-1538-4557-a6c0-7de701fd83e7 Sydney NSW

table 车

id  name     descr   listingOption priceStarting priceReserve  
83 Valiant Old Car 2 1000 1500
86 Volvo Safe Car 3 3000 4500

TABLE auction_images

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

查询-----

 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();

最佳答案

这是一项非常大的家庭作业。给你,

使用这个查询,

            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 },
};

关于c# - 复杂的 Linq C# 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13266580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com