简体   繁体   中英

Linq group by select

I have the following LINQ expression and I need to add two extrastring fields (newFieldWhat and newFieldBy) to Record-object in the result-list. The String-value could be either first or last, no difference. How can i achieve that?

List<Record> groupedTable = (from t in recordTable
                    group t by new {t.groupBy1}
                    into grp
                        select new
                        Record{
                            groupBy1 = grp.Key.groupBy1,
                            attribute1 = grp.Sum(t => t.attribute1),
                            newFieldWhat = ?
                            newFieldBy = ?
                        }).OrderBy(a => a.groupBy1).ToList();

This comment makes it clearer:

 newFieldWhat = grp.First(t => t.newFieldWhat) 

gives me: "cannot convert 'string' to 'bool'. newFieldWhat is a string"

I think you want:

newFieldWhat = grp.First().newFieldWhat

or, if you want all of the group:

newFieldWhat = String.Join(",", grp.Select(t => t.newFieldWhat))

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