2016-04-14 14 views
0

LINQでヘルプが必要です以下のクエリを実行してください。LINQ 3つのテーブルを結合するクエリ

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

} 

私は、上記の関数を書いたが、動作していない、それは代わりに

をフィルタリングする私は、以下の条件に

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) 

を満たすwhchすべてClientGroupsを取得するクエリを記述したいすべてのclientgroupsを取得しますどうぞお助けください。

答えて

1

あなただけの前のクエリから突出したこのライン

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

にソース2)からフィルタリングされていません。

私が正しく理解していれば、これをしたいと思う。

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

ありがとうございます。完璧に動作します。 – VVR147493

関連する問題