I'm sure it's something really stupid, but I just don't see it..
我确定这是非常愚蠢的事情,但我只是看不到它..
pData.LocationHours
is of type BaseLocationHoursDataSet.LocationHoursDataTable
. Yet when I hover the cursor over l
, all I see is "(range variable) TSource l
" - why is that?? Why doesn't linq know what it's dealing with? I try the same thing with a different DataTable and everything works fine.... not this guy. What could be the problem?
pData.LocationHours的类型为BaseLocationHoursDataSet.LocationHoursDataTable。然而,当我将光标悬停在l上时,我看到的只是“(范围变量)TSource l” - 为什么会这样? linq为什么不知道它正在处理什么?我尝试使用不同的DataTable做同样的事情,一切正常......不是这个人。可能是什么问题呢?
protected ListItem[] GetHoursTypesListItems(BaseLocationHoursDataSet pData)
{
return (
from l in pData.LocationHours // putting cursor over the l here shows: (range variable) TSource l
select new ListItem
{
Value = l, //ignore the fact that I didn't specify a property - that's the problem, that none of the properties of the object show up.
Text = l
}
).ToArray<ListItem>();
}
.
UPDATE:
The problem is that it doesn't know what l is. Instead of showing me the correct type (I expect to see LocationHoursRow), I see "TSource l".. What is that? Why doesn't it knwo what l is in the "from l in pData.LocationHours
" line?
问题是它不知道我是什么。而不是向我显示正确的类型(我希望看到LocationHoursRow),我看到“TSource l”..这是什么?为什么不知道“在l data in PData.LocationHours”中的l是什么?
4 个解决方案
#1
I think maybe you would need l.Field:
我想也许你需要l.Field:
select new ListItem
{
Value = l.Field,
Text = l.Field2
}
Okay how about something like this: Since you are using a data set your query may need to be similar to the following:
好的如此:由于您使用的是数据集,因此您的查询可能需要与以下内容类似:
var query = pData.Tables["Name"].AsEnumerable()
then do your LINQ off of the query object
然后从查询对象中删除LINQ
Also, found this code snippet that might help. It is using generic dataset for reference.
此外,找到了可能有用的代码段。它使用通用数据集作为参考。
DataSet ds = new DataSet();
FillOrders(ds);
DataTable orders = ds.Tables["SalesOrderHeader"];
var ordersQuery = orders.ToQueryable();
var query = from o in ordersQuery
where o.Field<bool>("OnlineOrderFlag") == true
select new { SalesOrderID = o.Field<int>("SalesOrderID"),
OrderDate = o.Field<DateTime>("OrderDate") };
#2
I see "TSource l".. What is that?
我看到“TSource l”..那是什么?
First, the compiler translates the query from query form into method call form. This query becomes
首先,编译器将查询从查询形式转换为方法调用形式。这个查询变成了
pData.LocationHours.Select(l=>new ... )
Second, the compiler attempts to determine what "pData.LocationHours.Select" means. If the type of pData.LocationHours does not have a Select method then the compiler starts looking for extension methods. Presumably it finds the extension method
其次,编译器试图确定“pData.LocationHours.Select”的含义。如果pData.LocationHours的类型没有Select方法,则编译器开始寻找扩展方法。据推测,它找到了扩展方法
IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source, Func<TSource, TResult> projection)
Or perhaps it finds the IQueryable version of the same.
或许它找到相同的IQueryable版本。
Now the compiler says "but what are the type parameters TSource and TResult?"
现在编译器说“但是TSource和TResult的类型参数是什么?”
I do not know what your problem is, but it is highly likely that the problem is occurring at this phase. The type inference engine is unable to determine what TSource is, for some reason.
我不知道你的问题是什么,但问题很可能发生在这个阶段。由于某种原因,类型推理引擎无法确定TSource是什么。
Now you hover over "l". What happens? The intellisense engine asks the semantic analyzer what the type of "l" is. The semantic analyzer reports that "l" is known to be a parameter in a function that takes a TSource and returns a TResult, but that the method type inferrer was unable to determine what actual type TSource corresponds to.
现在你将鼠标悬停在“l”上。怎么了? intellisense引擎询问语义分析器“l”的类型。语义分析器报告已知“l”是函数中的参数,该函数接受TSource并返回TResult,但是方法类型推导者无法确定TSource对应的实际类型。
So the intellisense engine does the best it can with what its got and tells you that l is of type TSource. The intellisense engine also notes that "l" is the range variable of a query, and tells you that fact as well.
所以intellisense引擎尽其所能,并且它告诉你l是TSource类型。智能感知引擎还注意到“l”是查询的范围变量,并且也告诉您这个事实。
Why doesn't it know what l is in the "from l in pData.LocationHours" line?
为什么它不知道l在“from l in pData.LocationHours”行中是什么?
I don't know but clearly something is broken in your code. Without knowing the types of all of the expressions and exactly what extension methods are available, it is hard for me to say what exactly has gone horribly wrong.
我不知道,但很明显你的代码中有些东西被破坏了。如果不知道所有表达式的类型以及确切的可用扩展方法,我很难说出究竟出现了什么错误。
When the code is broken and cannot compile, intellisense still does the best it can. I agree that in this case the result is a bit confusing, but at least you know that its getting as far as type inference before something goes wrong.
当代码被破坏而无法编译时,智能感知仍然能够做到最好。我同意在这种情况下结果有点令人困惑,但至少你知道它在出现错误之前就会进行类型推断。
#3
doesn't 'l' represent a data row here in the LocationHours table? I would think you'd need to specify the properties of 'l' in your lambda.
不会在LocationHours表中表示数据行吗?我认为你需要在lambda中指定'l'的属性。
#4
Should it be Value = l.Value or something like that?
它应该是Value = l.Value还是类似的东西?
#1
I think maybe you would need l.Field:
我想也许你需要l.Field:
select new ListItem
{
Value = l.Field,
Text = l.Field2
}
Okay how about something like this: Since you are using a data set your query may need to be similar to the following:
好的如此:由于您使用的是数据集,因此您的查询可能需要与以下内容类似:
var query = pData.Tables["Name"].AsEnumerable()
then do your LINQ off of the query object
然后从查询对象中删除LINQ
Also, found this code snippet that might help. It is using generic dataset for reference.
此外,找到了可能有用的代码段。它使用通用数据集作为参考。
DataSet ds = new DataSet();
FillOrders(ds);
DataTable orders = ds.Tables["SalesOrderHeader"];
var ordersQuery = orders.ToQueryable();
var query = from o in ordersQuery
where o.Field<bool>("OnlineOrderFlag") == true
select new { SalesOrderID = o.Field<int>("SalesOrderID"),
OrderDate = o.Field<DateTime>("OrderDate") };
#2
I see "TSource l".. What is that?
我看到“TSource l”..那是什么?
First, the compiler translates the query from query form into method call form. This query becomes
首先,编译器将查询从查询形式转换为方法调用形式。这个查询变成了
pData.LocationHours.Select(l=>new ... )
Second, the compiler attempts to determine what "pData.LocationHours.Select" means. If the type of pData.LocationHours does not have a Select method then the compiler starts looking for extension methods. Presumably it finds the extension method
其次,编译器试图确定“pData.LocationHours.Select”的含义。如果pData.LocationHours的类型没有Select方法,则编译器开始寻找扩展方法。据推测,它找到了扩展方法
IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source, Func<TSource, TResult> projection)
Or perhaps it finds the IQueryable version of the same.
或许它找到相同的IQueryable版本。
Now the compiler says "but what are the type parameters TSource and TResult?"
现在编译器说“但是TSource和TResult的类型参数是什么?”
I do not know what your problem is, but it is highly likely that the problem is occurring at this phase. The type inference engine is unable to determine what TSource is, for some reason.
我不知道你的问题是什么,但问题很可能发生在这个阶段。由于某种原因,类型推理引擎无法确定TSource是什么。
Now you hover over "l". What happens? The intellisense engine asks the semantic analyzer what the type of "l" is. The semantic analyzer reports that "l" is known to be a parameter in a function that takes a TSource and returns a TResult, but that the method type inferrer was unable to determine what actual type TSource corresponds to.
现在你将鼠标悬停在“l”上。怎么了? intellisense引擎询问语义分析器“l”的类型。语义分析器报告已知“l”是函数中的参数,该函数接受TSource并返回TResult,但是方法类型推导者无法确定TSource对应的实际类型。
So the intellisense engine does the best it can with what its got and tells you that l is of type TSource. The intellisense engine also notes that "l" is the range variable of a query, and tells you that fact as well.
所以intellisense引擎尽其所能,并且它告诉你l是TSource类型。智能感知引擎还注意到“l”是查询的范围变量,并且也告诉您这个事实。
Why doesn't it know what l is in the "from l in pData.LocationHours" line?
为什么它不知道l在“from l in pData.LocationHours”行中是什么?
I don't know but clearly something is broken in your code. Without knowing the types of all of the expressions and exactly what extension methods are available, it is hard for me to say what exactly has gone horribly wrong.
我不知道,但很明显你的代码中有些东西被破坏了。如果不知道所有表达式的类型以及确切的可用扩展方法,我很难说出究竟出现了什么错误。
When the code is broken and cannot compile, intellisense still does the best it can. I agree that in this case the result is a bit confusing, but at least you know that its getting as far as type inference before something goes wrong.
当代码被破坏而无法编译时,智能感知仍然能够做到最好。我同意在这种情况下结果有点令人困惑,但至少你知道它在出现错误之前就会进行类型推断。
#3
doesn't 'l' represent a data row here in the LocationHours table? I would think you'd need to specify the properties of 'l' in your lambda.
不会在LocationHours表中表示数据行吗?我认为你需要在lambda中指定'l'的属性。
#4
Should it be Value = l.Value or something like that?
它应该是Value = l.Value还是类似的东西?