Rene Koch
Rene Koch

Reputation: 337

Linq let not available in groupby

this is my query:

// stores all of the types we are interested in.
var desiredTypes = new[]
    {
        DBStoerungGlobalManager.enumStoerArt.Kommunikation,
        DBStoerungGlobalManager.enumStoerArt.Kaelte
    };

List<FaultStatisticModel> data2 = (from a in data
                                   let statisticType = (DBStoerungGlobalManager.enumStoerArt)a.FArt // cast and store the statistic type
                                   where desiredTypes.Contains(statisticType) // filter by the collection of desired types
                                   group a by a.FDatum.ToString(groupBy) into mg
                                   join b in dtList on mg.Key equals b.ToString(groupBy)
                                   select new FaultStatisticModel
                                   {
                                       Date = mg.Key,

                                       // make sure the correct property receives the result
                                       KommunikationValue = statisticType == DBStoerungGlobalManager.enumStoerArt.Kommunikation ? mg.Count() : 0,
                                       KaelteValue = statisticType == DBStoerungGlobalManager.enumStoerArt.Kaelte ? mg.Count() : 0
                                   }).ToList();

Sadly it doesnt work, the statistic type isnt available in the select new, I think it has something todo with my groupby before

Upvotes: 0

Views: 269

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460288

A group typically contains multiple, that's why you can't access the statisticType anymore. A group can contain multiple different types since you're not grouping by it

In fact I want to group by it..

You can use an anonymous type to group by two properties:

var data2 = from a in data
            let statisticType = (DBStoerungGlobalManager.enumStoerArt)a.FArt
            where desiredTypes.Contains(statisticType) // filter by the collection of desired types
            group a by new { Datum = EntityFunctions.TruncateTime(a.FDatum), Type = statisticType } into mg
            join b in dtList on mg.Key.Datum equals EntityFunctions.TruncateTime(b)
            select new FaultStatisticModel
            {
                Date = mg.Key.Datum,
                KommunikationValue = mg.Key.Type == DBStoerungGlobalManager.enumStoerArt.Kommunikation ? mg.Count() : 0,
                KaelteValue = mg.Key.Type == DBStoerungGlobalManager.enumStoerArt.Kaelte ? mg.Count() : 0
            };

List<FaultStatisticModel> data2List = data2.ToList(); 

But maybe you just want to count the records of a specific statistic-type, then use this approach:

var data2 = from a in data
            let statisticType = (DBStoerungGlobalManager.enumStoerArt)a.FArt
            where desiredTypes.Contains(statisticType) // filter by the collection of desired types
            group a by EntityFunctions.TruncateTime(a.FDatum) into mg
            join b in dtList on mg.Key equals EntityFunctions.TruncateTime(b)
            select new FaultStatisticModel
            {
                Date = mg.Key,
                KommunikationValue = mg.Count(x => (DBStoerungGlobalManager.enumStoerArt)x.FArt == DBStoerungGlobalManager.enumStoerArt.Kommunikation),
                KaelteValue = mg.Count(x => (DBStoerungGlobalManager.enumStoerArt)x.FArt == DBStoerungGlobalManager.enumStoerArt.Kaelte)
            };

Upvotes: 3

Related Questions