简体   繁体   中英

sql query/entity framework query not returning distinct values on two tables

I have a query I am running on two Tables (Table Customer and Table Vendor). On Table Customer I may have multiple rows of similar records. I am joining the two tables using the parameters below, however I noticed instances of Table Vendor Id duplicates in the query result. I am guessing this is because I have multiple similar records on Table Customer. Please can someone assist me with the right query to use to get distinct ID values on the join? Note that I am using this on entity framework core for a project. See query used on entity framework converted to sql below

SQL

select distinct * from Vendor e inner join Customer d on e.account = d.Cod_account
where (e.ActionStatus = 'Pending' and d.ActionStatus = 'Pending') and (d.Amt = e.Amount)
and (e.UploadTime = '05:00pm') and (e.Uploaddate = d.Calldate) order by d.Amt desc

ENTITY FRAMEWORK

await (from  e in _dbc.Vendor join d in _dbc.Customer on e.account equals d.Cod_account 
where (e.ActionStatus == "Pending" && d.ActionStatus == "Pending") && (d.Amt == e.Amount)
&& (e.UploadTime == uploadtime) && (e.Uploaddate == d.Calldate)
orderby d.Amt descending
      select new CustomerVend
       {
          Customer = d,
          Vendor = e
       }).Take(2000).Distinct().ToListAsync();

I've corrected your LINQ query. Distinct should be placed before Take .

var query = 
   from e in _dbc.Vendor 
   join d in _dbc.Customer on e.account equals d.Cod_account 
   where e.ActionStatus == "Pending" 
      && d.ActionStatus == "Pending"
      && d.Amt == e.Amount
      && e.UploadTime == uploadtime 
      && e.Uploaddate == d.Calldate
   orderby d.Amt descending
   select new CustomerVend
   {
      Customer = d,
      Vendor = e
   }

var result = await query.Distinct().Take(2000).ToListAsync();

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