【问题标题】:Split a string value in list using linq使用 linq 在列表中拆分字符串值
【发布时间】:2016-07-19 06:23:32
【问题描述】:

我有一个列表,其列值类似于“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();

【问题讨论】:

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


    【解决方案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();
    

    【讨论】:

    • 谢谢@Gilad。它适用于子字符串的情况。但是总而言之,当我尝试访问字段 Salary 时,它没有得到,我们只得到 e 和 j。你能帮我整理一下吗?
    • @SinoyDevassy - 啊,当然很抱歉......你需要将它添加到group new { /*here..*/} 而不是在by new 中 - 现在你按薪水分组 - 它是否使感觉?一般来说,我建议您为您的收藏品和物品使用正确的名称。它使人们更容易理解正在发生的事情
    • 是的......它的工作原理。我是 linq 的新手。谢谢你的建议。:-)
    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2019-05-05
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多