I must be doing something wrong, but WHAT ?
I am writing a dll that will be the DAL (data access layer) to sa
SQLServer database. The IEnumerable<T> collections that these
functions will hopefully return will be used to bind some asp.net
controls.
I write the first function:
public System.Collections.IEnumerable getCategories()
{
var results = from c in db.Categories
where ((c.CategoryID >= 1))
orderby c.CategoryName
select new { Name = c.CategoryName, ID =
c.CategoryID };
return results;
}
It works. I then want to add a "chose one" entry at the top of my
stack:
public System.Collections.IEnumerable getCategories()
{
var results = from c in db.Categories
where ((c.CategoryID >= 1))
orderby c.CategoryName
select new { Name = c.CategoryName, ID =
c.CategoryID };
return results.Union(new[] { new { Name = choseOne, ID =
-1 } }).AsQueryable().OrderBy(i => i.ID);
where choseOne is a constant string, like "*** Please select a
value***"
It won't work.
ODDITY#1:
- At work (.NET 4) it said something else (I do not remember the text
of the error). Anyway, I guessed form there that the fact that I add
something AT THE END is the problem.
- At home (.NET 3.5) it says "The type arguments for method
'System.Linq.Queryable.Union<TSource>(System.Linq.IQueryable<TSource>,
System.Collections.Generic.IEnumerable<TSource>)' cannot be inferred
from the usage. Try specifying the type arguments explicitly."
Hmmmmmmm.......
Anyway. I change it so I add my string in the beginning., like so:
public System.Collections.IEnumerable getCategories()
{
var startingText = new[] { new { Name = chooseOne, ID =
-1 } };
var results = from c in db.Categories
where ((c.CategoryID >= 1))
orderby c.CategoryName
select new { Name = c.CategoryName, ID =
c.CategoryID };
results =
startingText.Union(results).AsQueryable().OrderBy(i => i.ID);
return results;
}
Hurray, it works !!! Good. Now, quick, let's create my other
functions. I add the second simple function, just like the first one.
public System.Collections.IEnumerable getCustomers()
{
var startingText = new[] { new { Name = chooseOne, ID =
-1 } };
var results = from c in db.Customers
where (c.Country == "France")
orderby c.CustomerID
select new { Name = c.CompanyName, ID =
c.CustomerID };
results =
startingText.Union(results).AsQueryable().OrderBy(i => i.ID);
return results;
}
ODDITY#1:
This one does NOT even compile ! It underlines
"results.Union(startingText)" with the same message as above:
"The type arguments for method
'System.Linq.Queryable.Union<TSource>(System.Linq.IQueryable<TSource>,
System.Collections.Generic.IEnumerable<TSource>)' cannot be inferred
from the usage. Try specifying the type arguments explicitly."
Why does the first function work and the second, identical, does
not ?
Thank you
Radu