简体   繁体   中英

Complex Linq c# query

I tried this in LinqPad

I have the following data and linq query which should return only 1 record and 2 images but returns 2 records and 4 images.

I know i have something wrong but don't know what. I am looking for the users details + car details + images fro user: 0b3c2ba5-1538-4557-a6c0-7de701fd83e7

It has something to do with the bids as when I delete one of he bids for user (0b3c2ba5-1538-4557-a6c0-7de701fd83e7 ) i then get 1 record and 2 images

TABLES
------

TABLE bids

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 userdetails

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

TABLE cars

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  

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

I am looking for the users details + car details + images fro user: 0b3c2ba5-1538-4557-a6c0-7de701fd83e7

Judging by what you said in that line, your goals can be acieved by splitting the query into 3 separate and using Distinct function for car IDs.

The course of action is as follows:

  1. Find all distinct car IDs from the bid table for current user
  2. Find all images (and user/car details) corresponding to the data from step 1

The code would be something like this:

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

Then you can select the user details and images (in two separate queries).

This is very big home work. Here you go,

Use this query,

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

Note : This query can be written in better way.

Here is sample tables (I considered your tables as List for me):

Tables / Class

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

Data / Records

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM