Multiple Join and Group By LINQ

柔情痞子 提交于 2019-12-04 16:58:55

May be this will work- I hadnt checked.

Very Bad Code though:

List<CategoryFeatureGroupFeature> cfcgQ = new List<CategoryFeatureGroupFeature>();
        List<Category> cat = new List<Category>();
        List<Feature> fe = new List<Feature>();
        List<FeatureGroup> feag = new List<FeatureGroup>();

        var query = (from p in cfcgQ
                     join q in feag on p.FeatureGroup_Id equals q.Id
                     join r in fe on p.Feature_Id equals r.Id
                     where p.Category_Id == 1
                     select new { q.Name, q.Id, FeatureName = r.Name }).GroupBy(p => new { p.Name, p.FeatureName, p.Id }).ToList().OrderBy(p => p.Key.FeatureName).ThenBy(p => p.Key.Name);

I modified answer a bit, assuming that category can have multiple groups (hope my assumption is correct). And I used anonymous objects as return result, but intention should be clear.

/*** DATA ***/
IList<Feature> featuresList = new List<Feature> { 
        new Feature { Id = 13, Name = "Barras de Tejadilho" },
        new Feature { Id = 15, Name = "Retrovisores Aquecidos" },
        new Feature { Id = 16, Name = "Retrovisores Elétricos" },
        new Feature { Id = 7, Name = "Bancos Traseiros Rebatíveis" },
        new Feature { Id = 35, Name = "Computador de Bordo" },
        new Feature { Id = 38, Name = "Suporte para Telemóvel" },
        new Feature { Id = 1, Name = "2nd Exterior Feature" }
};
IList<FeatureGroup> featureGroupList = new List<FeatureGroup>{
    new FeatureGroup { Id = 1, Name = "Interior" },
    new FeatureGroup { Id = 2, Name = "Exterior" },
    new FeatureGroup { Id = 3, Name = "2nd Exterior" }
};
IList<Category> categoryList = new List<Category>{
    new Category{ Id=1, Name="All interior" },
    new Category { Id=2, Name="All exterior" }
};
IList<CategoryFeatureGroupFeature> cfcList = new List<CategoryFeatureGroupFeature>
{
    new CategoryFeatureGroupFeature { Category_Id = 1, FeatureGroup_Id = 1, Feature_Id = 7 },
    new CategoryFeatureGroupFeature { Category_Id = 1, FeatureGroup_Id = 1, Feature_Id = 35 },
    new CategoryFeatureGroupFeature { Category_Id = 1, FeatureGroup_Id = 1, Feature_Id = 38 },
    new CategoryFeatureGroupFeature { Category_Id = 2, FeatureGroup_Id = 2, Feature_Id = 13 },
    new CategoryFeatureGroupFeature { Category_Id = 2, FeatureGroup_Id = 2, Feature_Id = 15 },
    new CategoryFeatureGroupFeature { Category_Id = 2, FeatureGroup_Id = 2, Feature_Id = 16 },
    new CategoryFeatureGroupFeature { Category_Id = 2, FeatureGroup_Id = 3, Feature_Id = 1 }
};

/*** QUERY ***/
var result = from c in categoryList
                select new {
                    Id = c.Id,
                    Name = c.Name,
                    FeatureGroups = from fg in featureGroupList
                                    where (from cfc in cfcList
                                        where cfc.Category_Id == c.Id
                                        select cfc.FeatureGroup_Id).Distinct()
                                    .Contains(fg.Id)
                                    select new {
                                        Id = fg.Id,
                                        Name = fg.Name,
                                        Features = (from f in featuresList
                                                join cfc2 in cfcList on f.Id equals cfc2.Feature_Id
                                                where cfc2.FeatureGroup_Id == fg.Id
                                                select f).Distinct()
                                    }
                };

You could achive same result using combination of join and group by, but I went for Distinct().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!