var是关键字吧 class ImplicitlyTypedLocals2 { static void Main() { string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" }; // If a query produces a sequence of anonymous types, // then use var in the foreach statement to access the properties. var upperLowerWords = from w in words select new { Upper = w.ToUpper(), Lower = w.ToLower() }; // Execute the query foreach (var ul in upperLowerWords) { Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower); } } } /* Outputs: Uppercase: APPLE, Lowercase: apple Uppercase: BLUEBERRY, Lowercase: blueberry Uppercase: CHERRY, Lowercase: cherry */
可以赋予局部变量推断“类型”var 而不是显式类型。var 关键字指示编译器根据初始化语句右侧的表达式推断变量的类型。推断类型可以是内置类型、匿名类型、用户定义类型或 .NET Framework 类库中定义的类型。// i is compiled as an int var i = 5;// s is compiled as a string var s = "Hello";// a is compiled as int[] var a = new[] { 0, 1, 2 };// expr is compiled as IEnumerable<Customer> // or perhaps IQueryable<Customer> var expr = from c in customers where c.City == "London" select c;// anon is compiled as an anonymous type var anon = new { Name = "Terry", Age = 34 };// list is compiled as List<int> var list = new List<int>();
今天我也碰到了一个foreach循环不到的问题,最后改用for循环就ok了
class ImplicitlyTypedLocals2
{
static void Main()
{
string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" }; // If a query produces a sequence of anonymous types,
// then use var in the foreach statement to access the properties.
var upperLowerWords =
from w in words
select new { Upper = w.ToUpper(), Lower = w.ToLower() }; // Execute the query
foreach (var ul in upperLowerWords)
{
Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower);
}
}
}
/* Outputs:
Uppercase: APPLE, Lowercase: apple
Uppercase: BLUEBERRY, Lowercase: blueberry
Uppercase: CHERRY, Lowercase: cherry
*/
var i = 5;// s is compiled as a string
var s = "Hello";// a is compiled as int[]
var a = new[] { 0, 1, 2 };// expr is compiled as IEnumerable<Customer>
// or perhaps IQueryable<Customer>
var expr =
from c in customers
where c.City == "London"
select c;// anon is compiled as an anonymous type
var anon = new { Name = "Terry", Age = 34 };// list is compiled as List<int>
var list = new List<int>();
不提示任何错误
举例
strTableName[0]=“aa”;strTableName[0]=“bb”;foreach (string @var in strTableName)
{
DataTable shemaColumnsInfo3 = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, “aa”, null }); 返回的shemaColumnsInfo3.rows.count 为2DataTable shemaColumnsInfo4 = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, “bb”, null });
返回的shemaColumnsInfo3.rows4count 为4
DataTable shemaColumnsInfo1 = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, var, null });
返回的shemaColumnsInfo1.rows.count 为0
}
foreach 第一次进入 如果 @var=“aa”
DataTable shemaColumnsInfo3 = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, “aa”, null });
返回的shemaColumnsInfo3.rows.count 为2DataTable shemaColumnsInfo4 = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, “bb”, null });
返回的shemaColumnsInfo3.rows4count 为4DataTable shemaColumnsInfo1 = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, var, null });
返回的shemaColumnsInfo1.rows.count 为2
foreach 第二次进入 @var=“bb”
DataTable shemaColumnsInfo3 = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, “aa”, null });
返回的shemaColumnsInfo3.rows.count 为2DataTable shemaColumnsInfo4 = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, “bb”, null });
返回的shemaColumnsInfo3.rows4count 为4DataTable shemaColumnsInfo1 = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, var, null });
返回的shemaColumnsInfo1.rows.count 为0
------------------ 问题就是这样
你确定第二次进入的时候var是bb?加断电调试过了吗?
是写错了strTableName[1]=“bb”; 进入的是bb 所以很奇怪
new object[] { null, null, var, null }
这里吧
DataTable shemaColumnsInfo3 = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, "aa", null });
DataTable shemaColumnsInfo4 = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, "bb", null });
这两句删掉呢
你直接用for好了,另外,换个名字,不要用var
-------程序发过去了,请查收。GetColumnsInfo 这个方法测试都改乱了,您凑合看