简体   繁体   中英

Linq with multiple columns while doing a trim() on a column

I am trying to write a linq query that will allow me to do a trim on one of the columns that I am grouping by.

Here is what I have so far and it doesn't work I get 2 errors:

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();

Errors:

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

and

'AnonymousType#1' does not contain a definition for 'csname' and no extension method 'csname' accepting a first argument of type 'AnonymousType#1' could be found (are you missing a using directive or an assembly reference?)

I am attempting to do a trim on r.csname

new { r.type, r.csname }

is a shortname for

new { type = r.type, csname = r.csname }

With the new property taking the name of the property used. However, when you add the .Trim() , then it's no longer a direct use of a property, so there's no default name. What you want is:

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

That should solve both problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM