views:

322

answers:

4

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?

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?

A: 

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.

Phil Bennett
A: 

Should it be Value = l.Value or something like that?

Tom Clarkson
+4  A: 

I think maybe you would need 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

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") };
Jason Heine
Yes, but none of the properties show up when I type in 'l.' because it doesn't know what l is.
Kon
I added some extra comments to see if that helps lead you in the right direction
Jason Heine
Thanks! The AsEnumerable() did the trick. Then I just had to cast l for every reference of a property within it.
Kon
Awesome!! I am glad i could help
Jason Heine
+11  A: 

I see "TSource l".. What is that?

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

IEnumerable<TResult> Select<TSource, TResult>(
  this IEnumerable<TSource> source, Func<TSource, TResult> projection)

Or perhaps it finds the IQueryable version of the same.

Now the compiler says "but what are the type parameters TSource and 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.

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.

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.

Why doesn't it know what l is in the "from l in pData.LocationHours" line?

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.

Eric Lippert
I like your explanation - +1. And I do agree that something is broken - I concluded it had to do with the way the DataTable I was working with was implemented. I'm not proud of it, but I've gone around the issue with the help of AsEnumerable() as opposed to figuring out what's up with that DataTable (which I'd have to jump through quite a few political hoops to update/fix).
Kon
Fixed the code formatting. Hope you don't mind :)
Johannes Schaub - litb