Pergunta

I want to count all items in a list if a value is true and the count the false values.

I have this:

Items.GroupBy(
    i => i.ItemID,
    i => new { isScheduled = i.isScheduled },
    (key, g) => new ItemStatistics()
    {
        ItemID = key,
        ScheduledItems = g.Count(g=>g.isScheduled),
        UnscheduledItems = g.Count(g=> !g.isScheduled)
    }).ToList();

this gives me the following compilation error:

Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer' because it is not a delegate type

as if the it expects a different method overload

when i do this everything seems to be okay...

Items.GroupBy(
    i => i.ItemID,
    i => new { isScheduled = i.isScheduled },
    (key, g) => new ItemStatistics()
    {
        ItemID = key,
        ScheduledItems = g.Count(),
        UnscheduledItems = g.Count()
    }).ToList();

why is it when i remove the g=> !g.isScheduled expression from the count method it accepts it ?

Foi útil?

Solução

Found it ... ARGH !

when I can use "g" as the variable for my inner lambda expression inside the group becuse it refers to the original group "g". so i changed g=>g.isScheduled toi=>i.isScheduled

Items.GroupBy(
    i => i.ItemID,
    i => new { isScheduled = i.isScheduled },
    (key, g) => new ItemStatistics()
    {
        ItemID = key,
        ScheduledItems = g.Count(i=>i.isScheduled),
        UnscheduledItems = g.Count(i=> !i.isScheduled)
    }).ToList();

and now everything is okay

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top