What's wrong with this linq query?

I'm sure this is really stupid, but I just can't see it.

pData.LocationHours

has a type BaseLocationHoursDataSet.LocationHoursDataTable

. However, when I hover over l

, all I see is " (range variable) TSource l

" - why is that? Why doesn't LINK know what this is about? I am trying the same with another 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 the problem, that none of the properties of the object show up.
              Text = l
            }
          ).ToArray<ListItem>();
}

      

...

UPDATE:

The problem is, she doesn't know what l is. Instead of showing me the correct type (I expect to see LocationHoursRow), I see "TSource l". What is it? Why doesn't this mean that l is on the line " from l in pData.LocationHours

"?

0


a source to share


4 answers


I think you might want l.Field:

select new ListItem
{
   Value = l.Field,
   Text = l.Field2
}

      

Ok, how about something like this: Since you are using a dataset, your query might look like the following:

var query = pData.Tables["Name"].AsEnumerable()

      



then your LINQ will disconnect from the request object

Also, this piece of code might help. It uses a common 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") };

      

+5


a source


I see "TSource l". What is it?

First, the compiler translates the request from the request form to the method call form. This request will

pData.LocationHours.Select(l=>new ... )

      

Second, the compiler tries to figure out what "pData.LocationHours.Select" means. If the pData.LocationHours type does not have a select method, then the compiler starts looking for extension methods. Presumably it finds an extension method

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

      

Or perhaps it finds an IQueryable version of the same.

Now the compiler says "but what are the parameters like TSource and TResult?"



I don't know what your problem is, but it is very likely that the problem occurs at this stage. The type inference engine cannot determine which TSource for any reason.

Now you will hover your mouse over "l". What's happening? The intellisense engine asks the semantic parser what type "l" is. The semantic parser reports that "l" is known to be a parameter in a function that takes a TSource and returns a TResult, but that the type inferrer method was unable to determine which corresponds to the actual TSource type.

So intellisense engine does its best with it and tells you that l is of type TSource. The intellisense engine also notes that "l" is a query range variable and reports this fact as well.

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

I don't know, but there is clearly something broken in your code. Without knowing the types of all expressions and exactly what extension methods are available, it's hard for me to tell exactly what was going wrong.

When the code is broken and can't compile, intellisense does its best anyway. I agree that the result is a little confusing in this case, but at least you know that getting it to type inference before something goes wrong.

+12


a source


doesn't "l" represent a row of data here in the LocationHours table? I would think that you need to specify the "l" properties in your lambda.

0


a source


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

0


a source







All Articles