简体   繁体   English

在当前上下文中不存在。 变量声明

[英]Does not exist in the current context. Variable declaration

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] C:\Users\04albjoh\Downloads\VS code\app\Program.cs(20,30): 错误 CS0103: 当前上下文中不存在名称“客户”[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 };
 

You've copied this code from the C# Language Reference: Declaration statements .您已从C# 语言参考:声明语句中复制了这段代码。

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 .变量customers是至少具有CityNamePhone属性的任何类型的集合,但该代码块的要点是,当您将select转换为匿名类型时,您必须声明变量以将它们存储为var .

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定义一个 class Customer ,它具有城市、姓名和电话属性以及相关的数据类型,并将customers初始化为 class Customer的 object List<Customer> customers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM