2013-02-13 83 views
2

我需要生成这个数据模型(例如):基于实体多重联和集团通过LINQ

IList<FeatureGroupFeaturesDto> fcf = new List<FeatureGroupFeaturesDto>(); 

fcf.Add(new FeatureGroupFeaturesDto 
{ 
    FeatureGroup = new FeatureGroupDto { Id = 1, Name = "Interior" }, 
    Features = new List<FeatureDto> { 
     new FeatureDto { Id = 7, Name = "Bancos Traseiros Rebatíveis" }, 
     new FeatureDto { Id = 35, Name = "Computador de Bordo" }, 
     new FeatureDto { Id = 38, Name = "Suporte para Telemóvel" } 
    }, 
}); 

fcf.Add(new FeatureGroupFeaturesDto 
{ 
    FeatureGroup = new FeatureGroupDto { Id = 2, Name = "Exterior" }, 
    Features = new List<FeatureDto> { 
     new FeatureDto { Id = 13, Name = "Barras de Tejadilho" }, 
     new FeatureDto { Id = 15, Name = "Retrovisores Aquecidos" }, 
     new FeatureDto { Id = 16, Name = "Retrovisores Elétricos" } 
    }, 
}); 

public class Category 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

public class CategoryFeatureGroupFeature 
{ 
    [Key] 
    [Column("Category_Id", Order = 0)] 
    public int Category_Id { get; set; } 

    [Key] 
    [Column("FeatureGroup_Id", Order = 1)] 
    public int FeatureGroup_Id { get; set; } 

    [Key] 
    [Column("Feature_Id", Order = 2)] 
    public int Feature_Id { get; set; } 
} 

public class Feature 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

public class FeatureGroup 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

基本的想法是让的所有功能按特征组分组的类别。

编辑:

我试图把这个模型中的信息:

public class FeatureCategoryFeatures 
{ 
    public FeatureGroup FeatureGroup { get; set; } 
    public IList<Feature> Features { get; set; } 
} 

如何与LINQ和Entity Framework实现这一目标?

谢谢。

+0

你的linq查询现在看起来像什么? – wes 2013-02-13 13:27:34

+0

嗨,像这样,但我得到一些部分underlyned:var featuresDomain =(从cfg in categoryFeatureGroupFeaturesQuery其中cfg.Category_Id == categoryId 在cfg.FeatureGroup_Id featureGroupQuery中加入fg等于fg.Id 在featureQuery上加入cf cfg.Feature_Id equals f.Id group fg by fg.Name into groupedFeatures group f by f.Id into grp select f); – Patrick 2013-02-13 13:32:55

+0

http://stackoverflow.com/a/9276585/1620568 – wes 2013-02-13 14:00:27

回答

0

可能会工作 - 我没有检查。

非常糟糕,虽然代码:

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); 
+0

嗨,它返回一个列表N个FeatureGroups只有一个功能在每个组中,其中N是总功能的数量。是否有可能返回包含所有功能的组列表?谢谢 – Patrick 2013-02-14 16:05:15

0

我修改回答了一下,假设该类别可以有多个组(希望我的假设是正确的)。我使用匿名对象作为返回结果,但意图应该清楚。

/*** 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() 
            } 
       }; 

你可以使用joingroup by组合achive相同的结果,但我去Distinct()

+0

嗨,谢谢!令人惊讶的是你如何实现这一点:)我试图把你的结果放在我的IList 中,但它不工作,你能帮我吗?再一次非常感谢你。 – Patrick 2013-03-27 12:08:14