I am joining two datatables and selecting using LINQ. I am getting an error in the ‘orderby’ line. “The name ‘contact’ does not exist in the current context”. If I order by columns from the ‘customer’ variable it works, but not using columns from the ‘contact’ variable.
Then I removed the orderby line and tried to order it using lambda expression like:
orders.OrderBy(o => (string) o["ContactName"];
and I got an error “Cannot apply indexing with [] to an expression of type ‘AnonymousType#1’. I don’t want to create a new class just for sorting.
Any ideas how to do this kind of sorting. I will be doing a sort using multiple columns from both tables.
CODE:
var orders = (from customer in Customers.AsEnumerable()
join contact in Contacts.AsEnumerable()
on (int) customer["CustomerID"] equals (int) contact["CustomerID"] into outer
from contact in outer.DefaultIfEmpty()
orderby contact["ContactName"]
select new
{
ContactID = (int) contact["ContactID"]),
Name = (string) contact["ContactName"]),
City = (string) contact["City"])
});
There are some syntax problems(too many paranthesis, f.e.
contact["ContactID"])), this should work:Sidenote: i would use the strong type
Fieldextension method.