在一个列上执行trim()时使用多个列的Linq



我正试图写一个linq查询,这将允许我做一个修剪我分组的列之一。

这是我到目前为止所做的,它不起作用,我得到两个错误:

var statusGroup =
        (from r in cxt.unilists
         group r by new { r.type, r.csname.Trim() } status
         into sg
         select new ClaimListListTotalLinqPresentation
                    {
                         Type = sg.Key.type,
                         Status = sg.Key.csname,
                         ClaimCount = sg.Count(),
                         ClaimTotal = sg.Sum(x => x.claimtotal)
                    })                         
                    .Where(x => x.Type == "HOSP").ToList();

错误:

无效匿名类型成员声明符。匿名类型成员必须使用成员赋值、简单名称或成员访问来声明。

'AnonymousType#1'不包含'csname'的定义,并且无法找到接受类型'AnonymousType#1'的第一个参数的扩展方法'csname'(您是否缺少using指令或程序集引用?)

我正在尝试对r.csname做修剪

new { r.type, r.csname }

的缩写
new { type = r.type, csname = r.csname }

新属性采用所使用的属性的名称。但是,当您添加.Trim()时,它不再是直接使用属性,因此没有默认名称。你想要的是:

new { r.type, csname = r.csname.Trim() }

这样两个问题都解决了。

最新更新