Monday, October 22, 2007

Where I am in my linq investigation

The problem is that the anonymous type cannot be used in a method call parameter, and when I use IEnumerable I lose my field names. I only see my IEnumerable interfaces.
So, how do I get my field names back?
Fijiwalano Shun:
BrianMinister:
I am trying to figure out how to marshal a ling query result and even work with the resultset in general.
I have posted my questions as comments in the code below.
At the moment, I am at a dead standstill on linq until I can get past this.
namespace LinqWoes
{
class Program
{
static void Main(string[] args)
{
NorthwindsDataContext ctx = new NorthwindsDataContext();
var ACustomers = from ac in ctx.Customers
where ac.ContactName.Substring(0, 1) == "A"
select ac;
// ACustomers.? I have lost strong typing by using var.
// Should I use another type when declaring a return from a query?
DoSomethingWithA(ACustomers);
}
//How am I supposed to pass a linq query? This doesn't work.
bool DoSomethingWithA(var ac)
{
//Some code will go here.
}
}
}

As a matter of fact, C# is still a strong typed language (linq doesn't break the rule), so please don't mix up the keyword "var" in C# with the one in javascript.
In your case, var ACustomer will generate IL that is absolutely identical to the code: IEnumerable ACustomer.

The var keyword just make your life easier and let the compiler to infer from the context what ACustomer really is.

Scott has wrote a blog to explain this in details, see Understanding the
Var Keyword part of New "Orcas" Language Feature: Anonymous Types