Ameer Sadek
Ameer Sadek

Reputation: 1

C# LINQ GroupBy error: variable {name} of type > {type} > referenced from scope '', but it is not defined

I am writing a Linq query that will be applied to an SQL DB. This is the query:

var entitiesGroups = await _context.ProductsDetailsStatisticsView
        .GroupBy(p => p.Id, (productDetailsId, info) => new
        {
            Key = productDetailsId,
            Info = info
        })
        .ToListAsync();

I am getting the following error when this query is evaluated:

Exception: System.InvalidOperationException: variable 'info' of type 'System.Collections.Generic.IEnumerable`1[ProductDetailsStatisticsEntity]' referenced from scope '', but it is not defined

Upvotes: 0

Views: 68

Answers (1)

Svyatoslav Danyliv
Svyatoslav Danyliv

Reputation: 27461

Since you need all records, do grouping on the client side:

var entitiesGroups = (await _context.ProductsDetailsStatisticsView.ToListAsync())
    .GroupBy(p => p.Id, (productDetailsId, info) => new
    {
        Key = productDetailsId,
        Info = info
    })
    .ToList();

Upvotes: 1

Related Questions