Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
var Grdquery = from db in db.V_Salaries.Where(i => i.InOut_Flag == 0 && i.Year == cariyil).OrderByDescending(i => i.Id)
                           orderby db.Worker_Name, db.Year_Name

                           select new
                           {
                               db.Id,
                               db.Year,
                               db.Worker_Code,
                               db.Worker_Name,
                               db.Position_Name,
                               db.Year_Name,
                               db.Receipt_Date,
                               db.SgkSalary,
                               db.SgkAgi,
                               db.Salary,
                               db.Overtimes_Salary,
                               db.Food_Price,
                               db.Road_Price,
                               db.Currency,
                               db.Worker_Position,
                           };

                foreach (var row in Grdquery)
                {
                    DataRow r = Table.NewRow();
                    r["Id"] = row.Id;//0
                    r["Year"] = row.Year;//1
                    r["Worker_Code"] = row.Worker_Code;//2
                    r["Worker_Name"] = row.Worker_Name;//3
                    r["Position_Name"] = row.Position_Name;//4
                    r["Year_Name"] = row.Year_Name;//5
                    r["Receipt_Date"] = row.Receipt_Date;//6
                    r["SgkSalary"] = row.SgkSalary;//7
                    r["SgkAgi"] = row.SgkAgi;//8
                    r["Salary"] = row.Salary;//9
                    r["Overtimes_Salary"] = row.Overtimes_Salary;//10
                    r["Food_Price"] = row.Food_Price;//11
                    r["Road_Price"] = row.Road_Price;//12
                    r["Currency"] = row.Currency;//13
                    r["Worker_Position"] = row.Worker_Position;//14

                    Table.Rows.Add(r);
                }


What I have tried:

question :There are 45 staff. Each employee has at least one salary record for this year. But 10 personnel have 2 salaries. All the records come out in my query below. No problem with single records. But I want the last record of the staff with 2 records. How can we solve it?
Posted
Updated 15-Dec-19 21:02pm
v2
Comments
Member 13768545 20-Dec-19 6:25am    
the question is solved.
List<v_salaries> Grdquery = db.V_Salaries.Where(i => i.InOut_Flag == 0 && i.Year == cariyil).OrderBy(x => x.Worker_Name).ThenBy(x => x.Year_Name)
.GroupBy(g => g.Worker_Code, (Key, a) => a.OrderByDescending(b => b.Id).FirstOrDefault())
.ToList();

1 solution

Use the Enumerable.GroupBy Method (System.Linq) | Microsoft Docs[^] to restrict your data to staff with exactly two rows - then order them and select the last row.
 
Share this answer
 
Comments
Maciej Los 16-Dec-19 5:11am    
5ed!

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