如何从我分组到列表中的对象集合中获取单个属性?

时间:2018-01-11 20:46:09

标签: c# json linq

我有两个列表,我尝试根据Group =>加入ID和艺术家=>群ID。

当我展示群组时,我想要从艺术家列表中显示群组中的所有成员。

            var groupID = from Group in allGroups
                          join Artist in allArtists
                          on Group.Id equals Artist.GroupId
                          into Members
                          where Group.Id == id
                          select new
                          {
                              Group = Group.GroupName,
                              Members
                          };
            return Json(groupID);

当JSON数据在浏览器中呈现时,我从每个艺术家那里获得所有数据,我只想显示名称。见下文:

  

[{"组":" G-单元""会员":[{" ARTISTNAME":"劳埃德Banks"," realName":" Christopher Lloyd"," age":34," hometown":"纽约City"," groupId":2," group":null},{" artistName":" The Game",&# 34; realName":" Jayceon Taylor"," age":37," hometown":" Compton"," groupId":2," group":null},{" artistName":" 50 Cent"," realName":&# 34; Curtis Jackson"," age":41," hometown":"纽约市"," groupId":2, "组":空}]}]

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

这应该有用......

var groupID = from Group in allGroups
                      join Artist in allArtists
                      on Group.Id equals Artist.GroupId
                      into Members
                      where Group.Id == id
                      select new
                      {
                          Group = Group.GroupName,
                          Members = Members.Select(e => e.artistName)
                      };
        return Json(groupID);