BrianMinister1

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

Below is a clip from another site where I also posed this.

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



Re: LINQ Project General Marshalling a linq result and losing strong typed results

Fabrice MARGUERIE

You are right: when you use the var keyword, strong typing is preserved. Your mistake is that the type of your query's result is not IEnumerable, but IEnumerable<Customer>. This is what ensures that each element of the result enumeration is strongly-typed and you can get access to its members directly and get IntelliSense too.




Re: LINQ Project General Marshalling a linq result and losing strong typed results

Wael Farag

The Acostomers variable in your code is not a Costomer Entity instance, it's a query that still not run until you put it in a forech loop.

To pass query variables between methods (parameter or a return value) you should use the exact object type which is very easy in your code sample : IEnumerable

and your code can be written as folows:

Changed Code

namespace LinqWoes
{

class Program
{

static void Main(string[] args)
{
NorthwindsDataContext ctx = new NorthwindsDataContext();
IEnumerable<Customer> 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(IEnumerable<Customer> ac)
{

foreach( Customer in ac)

{

//Some code will go here.
}

}

}

}






Re: LINQ Project General Marshalling a linq result and losing strong typed results

BrianMinister1

So the solution is to Marshal the foreach results rather than the query itself.

However, getting a single result so far seems to be a pain, since it looks like I still need to run a foreach loop.

If anyone knows how to get a single row result without using a foreach loop, I would appreciate it.






Re: LINQ Project General Marshalling a linq result and losing strong typed results

Wael Farag

To Get a single result use the Single extention method instead of the query syntax

Code Block

namespace LinqWoes
{

class Program
{

static void Main(string[] args)
{
NorthwindsDataContext ctx = new NorthwindsDataContext();
Customer ACustomers =

ctx.Customers.Single(ac => ac.ContactName.Substring(0, 1) == "A");

// This will return only one record and one Customer instance

//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(Customer ac)
{

//Some code will go here.

}

}

}

This will return only one record and one Customer instance Hope that is what you want to get