3

我在 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 

表用户详细信息

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  

询问 - - -

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

2 回答 2

0

我正在寻找用户详细信息 + 汽车详细信息 + 用户图片:0b3c2ba5-1538-4557-a6c0-7de701fd83e7

根据您在该行中所说的话,您可以通过将查询分成 3 个单独的查询并使用不同的汽车 ID 函数来实现您的目标。

行动过程如下:

  1. 从当前用户的出价表中查找所有不同的汽车 ID
  2. 查找与步骤 1 中的数据对应的所有图像(和用户/汽车详细信息)

代码将是这样的:

var carIDs = bids.Where(x=>x.usedid == new Guid("0b3c2ba5-1538-4557-a6c0-7de701fd83e7")).Select(x=>x.cardid).Distinct();

然后您可以选择用户详细信息和图像(在两个单独的查询中)。

于 2012-11-11T15:49:37.447 回答
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 },
        };
于 2012-11-11T17:24:49.793 回答