I'm looking for a person to explain this code to me and tell why I'm getting this error.
C:\Users\04albjoh\Downloads\VS code\app\Program.cs(20,30): error CS0103: The name 'customers' does not exist in the current context [C:\Users\04albjoh\Downloads\VS code\app\app.csproj]
*// Example #1: var is optional when
// the select clause specifies a string
string[] words = { "apple", "strawberry", "grape", "peach", "banana" };
var wordQuery = from word in words
where word[0] == 'g'
select word;
// Because each element in the sequence is a string,
// not an anonymous type, var is optional here also.
foreach (string s in wordQuery)
{
Console.WriteLine(s);
}
// Example #2: var is required because
// the select clause specifies an anonymous type
var custQuery = from cust in customers
where cust.City == "Phoenix"
select new { cust.Name, cust.Phone };
// var must be used because each item
// in the sequence is an anonymous type
foreach (var item in custQuery)
{
Console.WriteLine("Name={0}, Phone={1}", item.Name, item.Phone);
}*
I tried adding
this: customers = {}
above
var custQuery = from cust in customers
where cust.City == "Phoenix"
select new { cust.Name, cust.Phone };
CodePudding user response:
You've copied this code from the C# Language Reference: Declaration statements.
That code is meant to be read, not compiled.
The variable customers
is any collection of types that at least have City
, Name
and Phone
properties, but the point of that code block is that when you select
into an anonymous type, you'll have to declare the variable to store those in as var
.
CodePudding user response:
Define a class Customer
which has properties City, Name and Phone with the relevant datatypes and initialize the customers
as the List of object of class Customer
like List<Customer> customers