开发者

Linq query and get aggregated result + extra info

I'm creating a linq query to get some data that will return a list of emails and some other info.

I've created the query so that i get the emails that i want, and the info. The thing is, it's possible that the info will be the same for more than 1 email.

I want to know what's the linq way to get a resultset that returns me something like

email1@domain.com,email2@domain.com  |   INFO
email3@domain.com  |   INFO2
email4@domain.com,email5@domain.com,email6@domain.com,email3@domain.com  |   INFO3

With the first property being a csv list of emails.

The query that I have right now is something like this :

from user in context.Users
join ufa in context.UFAccesses on user equals ufa.User
join f in context.F on ufa.Feed equals f
join fp in context.FP on f equals fp.F
select new { thisUfa = fp.F.UFAccess.Where(ufaSearch => ufaSearch.User == user && ufaSearch.F == f).FirstOrDefault(), user.Email, fp.Title, fp.Content };

EDIT : some extra info to help the answerer. maybe there's a better way to do this. the main reason for the question is, since i'm getting this INFO several times (because it may correspond to more than 1 email), i'm getting innecessary data. later, i can do a loop in开发者_开发问答 this resultset and for each info get a list of emails that correspond to it, but of course i'd like to get something more efficient and in linq if possible.


This one should solve your problem:

public class Something
{
    public string Email { get; set; }
    public string Info { get; set; }
}

var list = new List<Something>();
list.Add(new Something { Email = "email1", Info = "info1" });
list.Add(new Something { Email = "email2", Info = "info2" });
list.Add(new Something { Email = "email3", Info = "info3" });
list.Add(new Something { Email = "email4", Info = "info3" });
list.Add(new Something { Email = "email5", Info = "info3" });
list.Add(new Something { Email = "email6", Info = "info1" });

var groupedList = list.GroupBy(e => e.Info).Select(g => new { Info = g.Key, Emails = String.Join(",",g.Select(e => e.Email)) });

First we are grouping with GroupBy(e => e.Info) and then selecting group key (Info) and joined emails:

new { Info = g.Key, Emails = String.Join(",",g.Select(e => e.Email)) });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜