简体   繁体   中英

GroupBy in Entity Framework

I have one favoriteTalent Table

FavoriteTalentID                         TalentID             FavoriteID

101                                      54760                        1

102                                      54633                        1

103                                      54979                        1

104                                      54939                        9

105                                      54551                        9

106                                      54630                        3

107                                      54992                        3

108                                      54778                        7

109                                      60293                        7

110                                      53336                        4

I want to groupby the Favorite ID,Means i have to find out How many talentids are in specific FavoriteID

Like FavoriteID 1 has 3 TalentID

This is the query I have written, but output is not perfect.

I want to GroupBy the FavoriteID . That means I have to find out How many TalentIDs are in specific FavoriteID

var q = db.FavoriteTalents.Where(r => r.Favorite.CDUserID == UserID    
        ||r.Favorite.CDUserID == 0).GroupBy(t =>
new { t.TalentID, t.FavoriteID }
).Select(r => 
new { 

TalentID = r.Key.TalentID,
count = r.Count(),
CDUserID = r.FirstOrDefault().Favorite.CDUserID,
Title = r.FirstOrDefault().Favorite.Title}).ToArray();

return JsonResult(q);

You should group your collection by Favorite Id and then iterate groups to count distinct TalentIDs:

var q = db.FavoriteTalents
            .Where(r => r.Favorite.CDUserID == UserID || r.Favorite.CDUserID == 0)
            .GroupBy(t => t.FavoriteID)
            .Select(r => new { FavoriteID = r.Key, Count = r.Select(grouped => grouped.TalentID).Distinct().Count() })
            .ToArray();

GroupBy returns collection of IGrouping with key selected by selector from method parameter and collection of source objects with same result of selector function.

There is the same answer here .

UPDATE

To add "empty" Favorits to the result list you should iterate Favorits table:

var q = db.Favorites
            .Where(f => f.CDUserID == UserID || f.CDUserID == 0)
            .GroupBy(f => f.FavoriteID, f => f.FavoriteTalents)
            .Select(r => new { FavoriteID = r.Key, Count = r.SelectMany(grouped => grouped).Select(ft => ft.TalentID).Distinct().Count() })
            .ToArray();

It should be something like this:

foreach (string str in db.FavoriteTalents.GroupBy(c => c.FavoriteID)
                       .Select(group => new
                       {
                          fID = @group.Key,
                          Count = @group.Count()
                       }).OrderBy(x => x.fID).Select(item => String.Format("{0} ---> {1}", item.fID, item.Count)))
                     //Or even better with string interpolation : item => $"{item.fID} ---> {item.Count}"
{
     MessageBox.Show(str);
}

Result is:

1 ---> 3

9 ---> 2

3 ---> 2

7 ---> 2

4 ---> 1

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