score:10
con
isn't in scope in your grouping. Note that in the Microsoft example, their original query element was prod
, but after the grouping, they referred to prodGroup
.
Your query is grouping the elements together based upon a common Response
, you are essentially "losing" the individual con
for the greater whole. You may need to expand your grouping to include more criteria than just the Response, such as
group by new { con.Response, con.ActivityID }
and then refer to the ID via
gResponse.Key.ActivityID
Or perhaps access perhaps the first element's ActivityID inside each group
gResponse.First().ActivityID
Without more information as to what you are trying to accomplish, I cannot give specific advice for how you need to proceed.
score:1
You can't access con
within your new constructor due to scope. You need to use gResponse
to access the data you need.
Think of gResponse
as the sum of your parts; make sure that it includes everything you need access to when performing your select new
.
score:19
The into
clause is a query continuation clause. A query continuation removes all previous range variables from scope and then introduces a new range variable.
That may not be perfectly clear. Let me work up an example that demonstrates it better.
I have brown eyes; my sister has blue eyes. Suppose we wanted to find all the people like me: brown-eyed people with a blue-eyed sibling. We could do it like this:
var parentsWithABlueEyedChild =
from parent in parents
where parent.Children.Any(c=>c.EyeColor == Blue)
select parent;
var brownEyedChildren =
from parentWithABlueEyedChild in parentsWithABlueEyedChild
from child in parentWithABlueEyedChild.Children
where child.EyeColor == Brown
select child;
OK, you've got two queries. The second query operates on the results of the first query. Now, you agree here that "parent" is not in scope inside the second query, right? The "parent" range variable only has meaning within the query that declares it.
You can combine these two queries into one query like this:
var brownEyedChildren =
from parentWithABlueEyedChild in (
from parent in parents
where parent.Children.Any(c=>c.EyeColor == Blue)
select parent)
from child in parentWithABlueEyedChild.Children
where child.EyeColor == Brown
select child;
Again, it is clear here that "parent" is only in scope in the "inner" query, right?
But this syntax is hard to read compared to the first syntax; why is "parentWithABlueEyedChild" introduced three lines before it is used? The first version was more clear. We can make this into one query while maintaining the readability of the first version with a query continuation:
var brownEyedChildren =
from parent in parents
where parent.Children.Any(c=>c.EyeColor == Blue)
select parent into parentWithABlueEyedChild
from child in parentWithABlueEyedChild.Children
where child.EyeColor == Brown
select child;
That's exactly the same as the first two versions. The continuation is just a convenience. parent
is not in scope in the continuation clause, because it would not be in scope if you wrote them out as two queries.
Is it now clear why "con" is not in scope in your continuation? Your query
var q =
from con in contributions
group con by con.Response
into gResponse
select new
{
grop = gResponse.Count(),
con.ActivityID
};
is exactly the same as
var gResponses =
from con in contributions
group con by con.Response;
var q =
from gResponse in gResponses
select new
{
grop = gResponse.Count(),
con.ActivityID
};
"con" is not in scope inside the second query; it is only part of the first query.
score:0
Once you've done a group <something> by <function of something> into <group_variable>
, your original range variable <something>
is no longer available - all the something
s have been grouped up into group_variable
s, so that's all there is to talk about.
In your example, you start with a load of contributions, but then you group them up - now you have a load of groups, each of which has a Key
(the Response
used to group them) and a sequence of contributions. If you are happy taking the ActivityID
of the first of the contributions in a group (I don't know if this makes sense for you) you could do
select new
{
grop = gResponse.Count(),
ActivityID = gResponse.First().ActivityID
}
Source: stackoverflow.com
Related Articles
- The name 'con' does not exist in the current context
- The Name 'insert name here' does not exist in the current context
- Name does not exist in the current context but table is listed
- The name 'GimmePrimes' does not exist in the current context
- the name 'testinstances' does not exist in the current context
- How to avoid "The name 'ConfigurationManager' does not exist in the current context" error?
- "error CS0103: the name 'w' does not exist in the current context" in LINQ lambda expression
- Name doesn't exist in the current context LEFT OUTER JOIN in LINQ
- Does not exist in the current context
- The type or namespace name 'Linq' does not exist in the namespace 'System'
- The type or namespace name 'Linq' does not exist in the namespace 'System.Data'
- The type or namespace name 'Linq' does not exist
- Upgrade to 4.0: The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)
- DriveInfo does not exist in current context?
- LINQ function anonymous type not exist in current context
- The name 'XYZ' does not exist while group by LINQ Query
- LINQ - Proj does not exist in the current
- C# - code to order by a property using the property name as a string
- The data source does not support server-side data paging
- What does this C# code with an "arrow" mean and how is it called?
- A specified Include path is not valid. The EntityType does not declare a navigation property with the name *
- The member with identity ' ' does not exist in the metadata collection.\r\nParameter name: identity
- Does this LINQ code perform multiple lookups on the original data?
- Does ascending keyword exist purely for clarity when used with orderby?
- Why does getting a member expression member name differ between C# and VB.NET?
- Canonical Function "EntityFunctions.TruncateTime" does not exist in MYSQL
- Does this code really cause an "access to modified closure" problem?
- How does linq actually execute the code to retrieve data from the data source?
- LINQ Source Code Available
- Linq :DataTable select does not work if column name has space in it?
- Linq query select distinct Order
- sql select a default row if the result is not found
- StreamInsight complex query with filter on HoppingWindow
- Entity Framework - New child entities not added to database on saving parent entity (only in production environment)
- How can I define variables in LINQ?
- LINQ GroupBy x where there is more then one x and y is unique
- How to create SQL Database from Linq2Sql Model
- Select items by ID from another list of items - Local sequence cannot be used in LINQ to SQL
- Using templates and Create conflicts for creating a Person Object
- How to Execute a method inside LINQ to Entities'
- LinqToTwitter not seeing all private lists
- Parsing XML with LINQ in C#
- opening binary files from SQL using LINQ
- How to modify this LINQ statement to show bundles with count < 3 instead of CountWithinBundle < 3
- How to c# List<> order by and Group by according to parameters?
- Can 2 arrays be merged into an array of structures with 2 fields using only LINQ?
- How to map Expression<Func<TEntity, bool>> to Expression<Func<TDbEntity, bool>>
- What kind of query in LINQ do I have to make to get a custom List that contains Lists of objects?
- why I can't iterate through anonymous type
- Produce Insert SQL queries from a Dictionary using LINQ expressions