2

I need to generate this data model (example):

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" }
    },
});

Based on the Entities:

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; }
}

Basically the idea is to get all the Features of a Category grouped by FeatureGroup.

EDIT:

I'm trying to put the information in this model:

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

How can I achieve this with LINQ and Entity Framework ?

Thanks.

Patrick
  • 2,995
  • 14
  • 64
  • 125
  • What does your linq query look like right now? – wes Feb 13 '13 at 13:27
  • Hi, like this but I get some parts underlyned: var featuresDomain = (from cfg in categoryFeatureGroupFeaturesQuery where cfg.Category_Id == categoryId join fg in featureGroupQuery on cfg.FeatureGroup_Id equals fg.Id join f in featureQuery on cfg.Feature_Id equals f.Id group fg by fg.Name into groupedFeatures group f by f.Id into grp select f); – Patrick Feb 13 '13 at 13:32
  • http://stackoverflow.com/a/9276585/1620568 – wes Feb 13 '13 at 14:00
  • It does not have the Group By clause – Patrick Feb 13 '13 at 19:17
  • Teach a man to fish... – wes Feb 14 '13 at 12:46
  • @Patrick - see this link about bounties: http://meta.stackexchange.com/questions/16065/how-does-the-bounty-system-work - but you need 75 rep to be able to offer one. – Kev Mar 20 '13 at 17:14

2 Answers2

0

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);
Moons
  • 3,833
  • 4
  • 49
  • 82
  • Hi, it's returning a list a N FeatureGroups with only one feature in each group where N is the number of total features. Is it possible to return a list of groups with all the feature in it? thanks – Patrick Feb 14 '13 at 16:05
0

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

Nenad
  • 24,809
  • 11
  • 75
  • 93
  • Hi, Thanks! Amazing how you achieve this :) I'm trying to put your result in my IList but it's not working, can you help me on this? Thank you very much again. – Patrick Mar 27 '13 at 12:08