2016-04-14 20 views
0

Aşağıdakiler için LINQ Sorgusunda yardıma ihtiyacım var. LINQ Üç tabloya katılmak için sorgu

public interface IBrand 
{ 
     int BrandId { get; set; } 
     IEnumerable<IBuyingAgency> BuyingAgencies { get; set; } 
} 

public interface IBuyingAgency 
{ 
     int BuyingAgencyId { get; set; } 
} 

public interface IClientGroup 
{ 
     IBuyingAgency BuyingAgency { get; set; } 
     int ClientGroupId { get; set; } 
} 


1). var brands   = LoggedInUserHelper.GetUser().GetBrands(roles); // returns IEnumerable<Tuple<IBrand, string>> 
2). var buyingAgencies = LoggedInUserHelper.GetUser().GetBuyingAgencies(roles); //IEnumerable<IBuyingAgency> 
3). var clientGroups = LoggedInUserHelper.GetUser().GetClientGroups(roles); //IEnumerable<IClientGroup> 


function IEnumerable<IClientGroup> GetClientGroups(List<int> BrandIds) 
{ 
    var brands   = LoggedInUserHelper.GetUser().GetBrands(roles); // returns IEnumerable<Tuple<IBrand, string>> 
    var buyingAgencies = LoggedInUserHelper.GetUser().GetBuyingAgencies(roles); //IEnumerable<IBuyingAgency> 
    var clientGroups = LoggedInUserHelper.GetUser().GetClientGroups(roles); //IEnumerable<IClientGroup> 

    var lstBrandagencies = brands.Where(brand => BrandIds.Contains(brand.Item1.BrandId) && brand.Item1.BuyingAgencies.Any(ba => buyingAgencies.Contains(ba.BuyingAgencyId))).SelectMany(brand => brand.Item1.BuyingAgencies); 

    var buyingAgencyIDs = lstBrandagencies.Select(b => b.BuyingAgencyId); 

     clientGroups = clientGroups.Where(cg => buyingAgencyIDs.Contains(cg.BuyingAgency.BuyingAgencyId)); 

     return Mapper.Map<IEnumerable<IClientGroup>>(clientGroups.ToList());  

} 

Yukarıdaki fonksiyon yazdım ama çalışmıyor, bunun yerine filtreleme işleminin tamamını clientgroups alır

aşağıda koşul

1. retrieve the brand from brands (above) that matches the list of brandId's passing in as parameter 
2. Than get all the buyingAgencies under brands (1) above which matches with the id's of (2) above 
3. Finally get all clientgroups which matches with the buyingAgency retrieving in step (2) 

tatmin boyutunda tüm ClientGroups almak için bir sorgu yazmak istiyor

Lütfen yardım edebilir misin?

cevap

1

sadece önceki sorgudan projelendirme bu hat

var buyingAgencyIDs = lstBrandagencies.Select(b => b.BuyingAgencyId); 

daki kaynağından 2) den filtreleme değildir.

Eğer doğru anladıysam bunu yapmak istersiniz.

var lstBrandagencies = (from a in brands 
          where BrandIds.Contains(a.Item1.BrandId) 
          select a).SelectMany (b => b.Item1.BuyingAgencies) 
            .Select (b => b.BuyingAgencyId); 

    var buyingAgencyIDs = from a in buyingAgencies 
          where lstBrandagencies.Contains(a.BuyingAgencyId)       
          select a.BuyingAgencyId; 

    var clientGroupsResult = clientGroups.Where(cg => buyingAgencyIDs.Contains(cg.BuyingAgency.BuyingAgencyId)); 
+0

Teşekkür ederiz. Mükemmel çalışıyor. – VVR147493

İlgili konular