使用linq在列表中拆分字符串值

时间:2016-07-19 06:23:33

标签: c# asp.net linq linq-to-sql

我有一个列值,如" 0000000385242160714132019116002239344.ACK"我需要取这个值的最后6位数字,如" 239344"绑定到列表时没有扩展名(.ack)。 而且我还需要找到Salary字段的总和。

我的查询如下所示。

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 个答案:

答案 0 :(得分: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();