0

Below is the query,

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();

I am expecting list of LocatorDataModel in the result set. EngagementId is key which will be unique.

Currently it is pulling *2 records due to condition

(dcuri.URITypeID == (int)URITypeEnum.WebUri || dcuri.URITypeID == (int)URITypeEnum.AppUri)

How can get result like

Engagementid 1 : dataurl1 dataurl2
Engagementid 2: dataurl3 dataurl4, dataurl5

etc.

Any help is appreciated.

1
  • In your class ClientGroupUserEngagementModel, is LocatorDataModel a List? Commented Aug 29, 2017 at 3:01

1 Answer 1

1

I think below sample code can help you:

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();

That result will be:

[0]: { Engagementid = 1, Dataurls = "dataurl1,dataurl2" }
[1]: { Engagementid = 2, Dataurls = "dataurl3,dataurl4,dataurl5" }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.