Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've a Linq to sql query:

C#
IEnumerable<PY_History_TransactionTAB> FilteredReport;

var ReportData = db.PY_History_TransactionTAB.AsEnumerable()
                 .Where(x => x.SystemCode == SysCode)
                 .GroupBy(x => new 
                 { 
                   x.EmployeeCode,
                   x.EmployeeMaster.Emp_FullName,
                   x.Designations.Title,
                   department = x.Departments.Title
                 });

FilteredReport = ReportData.Select(x => new PY_History_TransactionTAB
                {
                    EmployeeCode = x.Key.EmployeeCode,
                    H_SalaryDays = x.Sum(y => y.H_SalaryDays ?? 0),
                    H_NET_Overtime = x.Sum(y => y.H_NET_Overtime),
                    H_Overtime_Amount = x.Sum(y => y.H_Overtime_Amount),
                    H_SL_Breakup1 = x.Sum(y => y.H_SL_Breakup1 ?? 0),                       
                    H_OT_Allowance1 = x.Sum(y => y.H_OT_Allowance1 ?? 0),                      
                }).ToList();


Now, if I want to add the GroupBy member x.EmployeeCode in Select(), it takes it, and works fine, but I don't know how to include a navigation property x.EmployeeMaster.Emp_FullName to Select(), which is a next member of GroupBy(). I know that the model PY_History_TransactionTAB to which I am refering in Select() does not contain definition for Emp_FullName but it contains definition for a navigation property i.e. EmployeeMaster.

So, Is there a way to include a navigation property x.EmployeeMaster.Emp_FullName to the Select() so that I can access them in a strongly typed view?

So, my question is that how do I modify my linq to sql query to get the result view right?

What I have tried:

I'm unaware of how to do this in c# , I've tried by putting the navigation property in the select() and it says that PY_History_TransactionTAB does not contain definition of Emp_FullName.
Posted
Updated 29-Sep-16 5:16am
v4
Comments
Richard Deeming 28-Sep-16 12:10pm    
Wouldn't x.Key.Emp_FullName work?
Member 12286283 28-Sep-16 17:17pm    
This is right, but how do I put it in a Select() as parameter, for employeecode I can say : EmployeeCode = x.Key.EmployeeCode, thats right but what about Emp_FullName, I can access it the way you mentioned, but where should I assign it, PY_HistoryTransactionTAB only contains definition for "EmployeeMaster", which is not a column itself but a navigation property to another table.
Richard Deeming 29-Sep-16 6:43am    
That's the problem with re-using your entity class as a DTO - if it doesn't match the structure of the data you want to return, you end up having to jump through hoops to make it work.

Try: EmployeeMaster = x.First().EmployeeMaster
Member 12286283 29-Sep-16 7:44am    
Perfect! That's the solution I was searching for, Thank you so much :) Can you put it as a solution below so that I can accept it as the answer.

As discussed in the comments, since you're reusing the PY_History_TransactionTAB class, and it doesn't contain an Emp_FullName property, you'll need to extract the EmployeeMaster from the first record in the group:
C#
FilteredReport = ReportData.Select(x => new PY_History_TransactionTAB
                {
                    EmployeeCode = x.Key.EmployeeCode,
                    EmployeeMaster = x.First().EmployeeMaster, // <-- Add this line.
                    H_SalaryDays = x.Sum(y => y.H_SalaryDays ?? 0),
                    H_NET_Overtime = x.Sum(y => y.H_NET_Overtime),
                    H_Overtime_Amount = x.Sum(y => y.H_Overtime_Amount),
                    H_SL_Breakup1 = x.Sum(y => y.H_SL_Breakup1 ?? 0),                       
                    H_OT_Allowance1 = x.Sum(y => y.H_OT_Allowance1 ?? 0),                      
                }).ToList();
 
Share this answer
 
Comments
Maciej Los 29-Sep-16 14:50pm    
5ed!
If i understand you correctly...

Assuming that Emp_FullName field doesn't belong to PY_History_TransactionTAB table, you have to join EmployeeMaster table to be able to access it.

See:
How to: Combine Data with LINQ by Using Joins (Visual Basic)[^]
join clause (C# Reference)[^]
Basic LINQ Query Operations (C#)[^]
How to: Perform Grouped Joins (C# Programming Guide)[^]
How to: Perform Inner Joins (C# Programming Guide)[^]
How to: Perform Left Outer Joins (C# Programming Guide)[^]
 
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