I’m trying to make a group by statement using LINQ. I am getting an error The name ‘con’ does not exist in the current context.
In my code after the select, I trying to get the ActivityID value but the list ‘con’ unavailable.
List<Contribution> contributions =
contLogic.GetAllContibutionsByActivityIDToList(ActivityID);
var groups = from con in contributions
group con by con.Response
into gResponse
select new
{
grop = gResponse.Count(),
con.ActivityID
};
I have look at Microsoft samples, and I don’t know what I did wrong.
Microsoft sample:
List<Product> products = GetProductList();
var orderGroups = from prod in products
group prod by prod.Category into prodGroup
select new { Category = prodGroup.Key, Products = prodGroup };
ObjectDumper.Write(orderGroups, 1);
conisn’t in scope in your grouping. Note that in the Microsoft example, their original query element wasprod, but after the grouping, they referred toprodGroup.Your query is grouping the elements together based upon a common
Response, you are essentially “losing” the individualconfor the greater whole. You may need to expand your grouping to include more criteria than just the Response, such asand then refer to the ID via
Or perhaps access perhaps the first element’s ActivityID inside each group
Without more information as to what you are trying to accomplish, I cannot give specific advice for how you need to proceed.