c# - group by multiple Columns and Select specific columns -
how can groupby multiple columns in linq , want take of columns:
public class notification { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public long notificationid { set; get; } public notificationtype notificationtype { set; get; } public int userid { get; set; } public userprofile userprofile { get; set; } public datetime createdate { set; get; } public bool notice { set; get; } public long newsid { set; get; } public int recipientid { get; set; } }
i have written following code, displays records:
var x=(from n in _notification n.recipientid == id orderby n.createdate descending, n.userid group new { n.userid, n.newsid} new { n.userid, n.newsid, n.notice, n.notificationtype, n.createdate, n.notificationid, n.userprofile } g select new notificationviewmodel { newsid = g.key.newsid, causerid = g.key.userid, causername = g.key.userprofile.name, createdate = g.key.createdate, notice = g.key.notice, notificationid = g.key.notificationid, notificationtype = g.key.notificationtype, }).tolist();
i want grouped according userid , newsid
you 1 listed instead of grouped list because @ end have select statement while should have group @ end. change code this:
var x=(from n in _notification n.recipientid == id orderby n.createdate descending, n.userid group new notificationviewmodel { newsid = g.key.newsid, causerid = g.key.userid, causername = g.key.userprofile.name, createdate = g.key.createdate, notice = g.key.notice, notificationid = g.key.notificationid, notificationtype = g.key.notificationtype, } new { n.userid, n.newsid}).tolist();
Comments
Post a Comment