Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to join 2 tables and retrieve distinct rows based on 1 columns in entity framework.

I have read posts on web including SO.

In the code below,
1. I join 2 tables based on CurrentTurnID key
2. I filter the table by the features that I want
3. I filter the table by setting conditions on selected features
4. I select a column to calculate the sum of all it's value

I need to retrieve records that have the same CurrentTurnID just once. Here's my code:

int services = Convert.ToInt32(database.Tbl_CurrentTurn.AsNoTracking()
            .Join(database.Tbl_pay.AsNoTracking(), f => f.CurrentTurnID,
               s => s.CurrentTurnID, (f, s) => new
               {
                   f.CurrentTurnOfficialID,
                   f.CurrentTurnID,
                   f.SickID,
                   f.Remove,
                   f.Payment,
                   f.ServicePayment,
                   s.isDeposit
               })
            .Where(w => w.SickID == SickId && w.Remove != true && w.Payment == true && w.isDeposit != true)
            .Select(s => s.ServicePayment).DefaultIfEmpty(0).Sum());


If I add Distinct() after Select() statement, it would consider the ServicePayment to filter distinct rows.

The Image is attached to my Dropbox. The 2nd and the 3rd records have the same CurrentTurnID value. So I want to consider just one of them. Then sum up records on ServicePayment column. Please notice that other columns are different in these 2 records.

What I have tried:

I have read posts on the web. Non of the solutions helped me.
Posted
Updated 1-May-19 22:29pm
Comments
Christian Graus 2-May-19 4:29am    
Try using group by? Then you can use max, if the values are all the same (or min)

1 solution

Try using group by? Then you can use max, if the values are all the same (or min)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900