Linq查询获取列表

时间:2017-08-29 02:15:09

标签: c# linq

以下是查询

List<int> groupIdList = {1, 2};

var clientGroupData = from cge in base.context.Set<ClientGroupEngagement>()
                      join cg in base.context.Set<ClientGroup>() on cge.ClientGroupID equals cg.ClientGroupID
                      join cgu in base.context.Set<ClientGroupUser>() on cg.ClientGroupID equals cgu.ClientGroupID
                      join eng in base.context.Set<Engagement>() on cge.EngagementID equals eng.EngagementID
                      join cdc in base.context.Set<CountryDataCenter>()
                      on new { eng.CountryID, eng.EngagementVersion } equals new { cdc.CountryID, cdc.EngagementVersion }
                      join dc in base.context.Set<DataCenter>()
                      on cdc.DataCenterID equals dc.DataCenterID
                      join dcuri in base.context.Set<DataCenterURI>()
                      on new { dc.DataCenterID, cdc.EngagementVersion } equals new { dcuri.DataCenterID, dcuri.EngagementVersion }
                      join uritype in base.context.Set<URIType>()
                      on dcuri.URITypeID equals uritype.URITypeID
                      where groupIdList.Contains(cgu.ClientGroupID)
                            && cg.IsActive
                            && cdc.IsActive
                            && dc.IsActive
                            && dcuri.IsActive
                            && uritype.IsActive
                            && (dcuri.URITypeID == (int)URITypeEnum.WebUri || dcuri.URITypeID == (int)URITypeEnum.AppUri)
                      select new ClientGroupUserEngagementModel
                      {
                          EngagementId = cge.EngagementID,
                          EngagementDescription = eng.EngagementDescription,
                          EngagementStatusId = eng.EngagementStatusID,
                          ClientGroupId = cg.ClientGroupID,
                          ClientGroupGuid = cg.ClientGroupGUID,
                          ClientGroupName = cg.ClientGroupName,
                          UserId = cgu.ClientUserID,
                          FirstName = cgu.FirstName,
                          LastName = cgu.LastName,
                          IsGroupUserActive = cgu.IsActive,
                          LocatorDataModel = new LocatorDataModel
                          {
                              DatacenterId = cdc.DataCenterID,
                              DatacenterName = dc.DataCenterName,
                              EngagementVersion = cdc.EngagementVersion,
                              Uri = dcuri.URI,
                              UriTypeId = dcuri.URITypeID
                          },
                      };

var result = await clientGroupData.ToListAsync();

我期待结果集中的LocatorDataModel列表。 EngagementId是唯一的关键。

目前,由于条件

,它正在拉动* 2条记录
(dcuri.URITypeID == (int)URITypeEnum.WebUri || dcuri.URITypeID == (int)URITypeEnum.AppUri)

如何获得像

这样的结果
Engagementid 1 : dataurl1 dataurl2
Engagementid 2: dataurl3 dataurl4, dataurl5

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我认为以下示例代码可以帮助您:

var list = new[]
{
    new { Engagementid = 1, Dataurl = "dataurl1"},
    new { Engagementid = 1, Dataurl = "dataurl2"},
    new { Engagementid = 2, Dataurl = "dataurl3"},
    new { Engagementid = 2, Dataurl = "dataurl4"},
    new { Engagementid = 2, Dataurl = "dataurl5"}
};

var result =
    list.GroupBy(g => g.Engagementid)
        .Select(c => new {Engagementid = c.Key, Dataurls = string.Join(",", c.Select(x=> x.Dataurl).ToList())})
        .ToList();

结果将是:

[0]: { Engagementid = 1, Dataurls = "dataurl1,dataurl2" }
[1]: { Engagementid = 2, Dataurls = "dataurl3,dataurl4,dataurl5" }