0

I have a list with a column value like "0000000385242160714132019116002239344.ACK" i need to take last 6 digits from this value like "239344" without extension(.ack) when binding to the list. And i need to find the sum of Salary field also.

My query looks like below.

var result = from p in Context.A
                             join e in B on p.Id equals e.Id
                             join j in Context.C on e.CId equals j.CId
                             where (e.Date >= periodFrom && e.Date <= periodTo)
                             group new
                             {
                                 e,
                                 j
                             } by new
                             {
                                 j.J_Id,
                                 e.Date,
                                 e.Es_Id,
                                 e.FileName,
                                 j.Name,
                                 e.ACK_FileName,
                                 p.EmpSalaryId,
                                 p.Salary
                             } into g
                             orderby g.Key.CId, g.Key.Es_Id, g.Key.Date, g.Key.FileName
                             select new
                             {
                                 CorporateId = g.Key.CId,
                                 ProcessedDate = g.Key.Date,
                                 EstID = g.Key.Es_Id,
                                 FileName = g.Key.FileName,
                                 Name = g.Key.Name,
                                 ack = g.Key.ACK_FileName,
                                 EmpSalaryId = g.Key.EmpSalaryId,
                                 Salary=g.Key.Salary
                             };

var Abc=result.ToList();

1 Answer 1

1
var result = (from p in Context.A
             join e in B on p.Id equals e.Id
             join j in Context.C on e.CId equals j.CId
             where (e.Date >= periodFrom && e.Date <= periodTo)
             group new { e, j } by new
             {
                 j.J_Id,
                 e.Date,
                 e.Es_Id,
                 e.FileName,
                 j.Name,
                 ACK_FileName = e.ACK_FileName.Substring(e.ACK_FileName.IndexOf(".ACK") - 7, 11),
                 p.EmpSalaryId,
                 p.Salary
             } into g
             orderby g.Key.CId, g.Key.Es_Id, g.Key.Date, g.Key.FileName
             select new
             {
                 CorporateId = g.Key.CId,
                 ProcessedDate = g.Key.Date,
                 EstID = g.Key.Es_Id,
                 FileName = g.Key.FileName,
                 Name = g.Key.Name,
                 ack = g.Key.ACK_FileName,
                 EmpSalaryId = g.Key.EmpSalaryId,
                 Salary = g.Sum(item => item.Salary)
             }).ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

Thank You @Gilad.Its working for the substring case. But for sum ,when i tried to access the field Salary its not getting,there we getting only e and j. Could you pls help me to sortout.
@SinoyDevassy - Ahh sorry of course.... You need to add it to the group new { /*here..*/} and not have it in the by new - now you are grouping by the Salary - does it make sense? In general I'd suggest you to use proper names for your collections and items. It makes it much easier to understand what is going on
Yeah...It works. I am new in linq . Thank you for the suggestion.:-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.