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: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
}
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.
Source: stackoverflow.com
Related Query
- 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
- 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?)
- Name doesn't exist in the current context LEFT OUTER JOIN in LINQ
- The name 'XYZ' does not exist while group by LINQ Query
- LINQ - Proj does not exist in the current
- The data source does not support server-side data paging
- 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
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- A specified Include path is not valid. The EntityType 'SpiceShop.Models.Product' does not declare a navigation property with the name 'Products'
- Entity Framework/LINQ Error: The column prefix 'Project1' does not match with a table name or alias name used in the query
- DriveInfo does not exist in current context?
- Linq Boolean returns exception DROPDOWNLIST has a SelectedValue which is invalid because it does not exist in the list of item
- VB.Net Linq with datatables - select from one table what does not exist in the other
- LINQ function anonymous type not exist in current context
- Delete the value in the database if the file does not exist
- Linq does not exist in the namespace System
- C# Linq with datatables - select from one table what does not exist in the other
- Get all the records where it does not exist in 2nd LINQ
- The required column 'int_CommodityPriceId' does not exist in the results. linq to sql
More Query from same tag
- How to create IQueryable collections?
- Linq Lambda expression for scalar value (max)
- c# linq search an EntityObject values from an IQueryable list
- How to get Linq data context for this function?
- LINQ Left outer join - Object reference not set to an instance of an object
- Include entities on a relationship with a custom property in EF Core
- LINQ .ToDictionary for 2d dictionary
- How can I turn these LINQ joins into LEFT OUTER joins?
- Generate all possible coverage options
- Linq to Entity | Pass table join to model
- How to retrieve one table row and list of rows that are connected to the first row in Linq .NET
- Sorting list throws KeyNotFoundException
- Inserting multiple custom items into listview
- How to create record on cascade tables with linq in mvc
- Strange .Where() behaviour. Somebody has an explanation?
- LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression."}
- Reading child nodes from xml string using C#, LINQ
- C#/Linq - Sort IDictionary
- Best way to write DataTables from a Web Service into XML?
- Exception on XML sorting
- How can I order in Linq Select Distinct if ordered by an Included entity?
- Providing a generic key comparison based on a collection of a generic type
- Linq Sql Database Context MissingMethod
- Querying n:m-Relationships with LINQ
- How can I validate data against injection attack when tables are varchar()
- How to flatten tree via LINQ?
- Linq C# Groupby on datatable
- How to find an element in Dictionary with LINQ
- How to reference a specific object from a list in a config file using .NET MVC with C#
- Does listOfLists contain at least one list that has one or more items?