简体   繁体   English

此linq查询出了什么问题?

[英]What's wrong with this linq query?

I'm sure it's something really stupid, but I just don't see it.. 我敢肯定这确实很愚蠢,但我只是看不到..

pData.LocationHours is of type BaseLocationHoursDataSet.LocationHoursDataTable . pData.LocationHours的类型为BaseLocationHoursDataSet.LocationHoursDataTable Yet when I hover the cursor over l , all I see is " (range variable) TSource l " - why is that?? 但是,当我将光标悬停在l ,我看到的只是“ (range variable) TSource l ”-为什么? Why doesn't linq know what it's dealing with? linq为什么不知道要处理什么? I try the same thing with a different DataTable and everything works fine.... not this guy. 我用不同的DataTable尝试相同的操作,但一切正常。 What could be the problem? 可能是什么问题呢?

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. 问题在于它不知道l是什么。 Instead of showing me the correct type (I expect to see LocationHoursRow), I see "TSource l".. What is that? 我没有显示正确的类型(我希望看到LocationHoursRow),而是看到“ TSource l”。那是什么? Why doesn't it knwo what l is in the " from l in pData.LocationHours " line? 为什么不知道pData.LocationHours中的l中的from l in pData.LocationHours l是什么?

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. 其次,编译器尝试确定“ pData.LocationHours.Select”的含义。 If the type of pData.LocationHours does not have a Select method then the compiler starts looking for extension methods. 如果pData.LocationHours类型没有Select方法,则编译器将开始寻找扩展方法。 Presumably it finds the extension method 大概找到了扩展方法

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". 现在,您将鼠标悬停在“ l”上。 What happens? 怎么了? The intellisense engine asks the semantic analyzer what the type of "l" is. 智能感知引擎询问语义分析器“ l”的类型是什么。 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”是采用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. 因此,智能感知引擎会尽力而为,并告诉您l属于TSource类型。 The intellisense engine also notes that "l" is the range variable of a query, and tells you that fact as well. 智能感知引擎还注意到“ l”是查询的范围变量,并且也告诉您该事实。

Why doesn't it know what l is in the "from l in pData.LocationHours" line? 为什么它不知道“ pData.LocationHours中的l中的l”中的l是什么?

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. 当代码被破坏并且无法编译时,Intellisense仍会尽力而为。 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. 我同意,在这种情况下,结果有点令人困惑,但是至少您知道,在出现问题之前,它会尽可能地进行类型推断。

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") };

doesn't 'l' represent a data row here in the LocationHours table? 'l'代表LocationHours表中的数据行吗? I would think you'd need to specify the properties of 'l' in your lambda. 我认为您需要在lambda中指定“ l”的属性。

应该是Value = l.Value还是类似的东西?

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

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