Linq casting and marshalling problems

Last post 11-08-2007 9:58 PM by BrianMinister. 9 replies.

Sort Posts:

  • Embarrassed [:$] Linq casting and marshalling problems

    10-19-2007, 9:00 PM
    • Loading...
    • BrianMinister
    • Joined on 10-19-2007, 8:49 PM
    • Orange County, CA
    • Posts 13

    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. Is thsi what I am supposed to use lamda expressions for?

          // 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.
        
    }
      }
    }

    初めは容易である-継続は堅い。
    Beginning is easy - Continuing is hard.
    http://BriansCodeExtreme.blogspot.com
    Filed under: , , , , ,
  • Re: Linq casting and marshalling problems

    10-20-2007, 5:44 AM
    • Loading...
    • Dested
    • Joined on 10-11-2006, 6:26 AM
    • Phoenix
    • Posts 166

    I ran into the same brick wall a bit ago when c#3.0 first premiered and all the response i got was that you cant. Does anyone know better?

     

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Linq casting and marshalling problems

    10-20-2007, 2:10 PM
    • Loading...
    • BrianMinister
    • Joined on 10-19-2007, 8:49 PM
    • Orange County, CA
    • Posts 13

    If you can't marshal the data from a DLinq query, then why use it?

    I thought from the beginning that Linq sounded like a half thought out concept, this will be proven true if it cannot be marshalled.

    初めは容易である-継続は堅い。
    Beginning is easy - Continuing is hard.
    http://BriansCodeExtreme.blogspot.com
  • Re: Linq casting and marshalling problems

    10-20-2007, 8:41 PM
    • Loading...
    • Dested
    • Joined on 10-11-2006, 6:26 AM
    • Phoenix
    • Posts 166

    Yah, this is how I felt too... Can someone prove me wrong?     

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Linq casting and marshalling problems

    10-22-2007, 5:13 AM
    Answer

    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. Is thsi what I am supposed to use lamda expressions for?

          // 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.
        
    }
      }
    }

    Hi buddies,

    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<Customer> 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

    Thanks

  • Re: Linq casting and marshalling problems

    10-22-2007, 1:59 PM
    • Loading...
    • BrianMinister
    • Joined on 10-19-2007, 8:49 PM
    • Orange County, CA
    • Posts 13

     Apparently I did not make myself clear.

    The problem is that the anonymous type cannot be used in a method call parameter, and when I use IEnumerable<Customer> 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. Is thsi what I am supposed to use lamda expressions for?

          // 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.
        
    }
      }
    }

    Hi buddies,

    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<Customer> 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

    Thanks

    初めは容易である-継続は堅い。
    Beginning is easy - Continuing is hard.
    http://BriansCodeExtreme.blogspot.com
  • Re: Linq casting and marshalling problems

    10-22-2007, 10:51 PM
    • Loading...
    • BrianMinister
    • Joined on 10-19-2007, 8:49 PM
    • Orange County, CA
    • Posts 13

    Here is the solution to my earlier issue.
    DLinq queries seem to only execute against a foreach loop.
    Now I can be insulting and pointout that forcing a foreach for a single role result is stupid.

    Ok, I may be over simplifying. There are probably other ways to get a single rows.
    At least my biggest puzzle is solved. Don't marshal queries, marshal results. Use "var" to contain queries, then strong type the results to get the data. 

    BrianMinister:

    Apparently I did not make myself clear.
    The problem is that the anonymous type cannot be used in a method call parameter, and when I use IEnumerable<Customer> 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. Is thsi what I am supposed to use lamda expressions for?
          // 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<Customer> 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

    初めは容易である-継続は堅い。
    Beginning is easy - Continuing is hard.
    http://BriansCodeExtreme.blogspot.com
  • Re: Linq casting and marshalling problems

    10-24-2007, 11:51 AM
    • Loading...
    • DkUltra
    • Joined on 03-19-2007, 2:06 PM
    • South Dakota / Nebraska
    • Posts 295

    Try
    //How am I supposed to pass a linq query? This doesn't work.
        bool DoSomethingWithA(yournamespace.linq.Customers cust)
        {
          //Some code will go here.
        }

    var ACustomers = (from ac in ctx.Customers
          where ac.ContactName.Substring(0, 1) == "A").firstordefault();
          select ac)  ---- not needed unless multible rows comming in ?  

     

    now ACustomers should have strong typing


    Hope this helps

    DK

  • Re: Linq casting and marshalling problems

    10-30-2007, 12:19 AM
    • Loading...
    • BrianMinister
    • Joined on 10-19-2007, 8:49 PM
    • Orange County, CA
    • Posts 13

    "NorthwindsDataContext.Customers ac" is not possible.

    NorthwindsDataContext.Customers type does not exist. it is a property not a type.

    初めは容易である-継続は堅い。
    Beginning is easy - Continuing is hard.
    http://BriansCodeExtreme.blogspot.com
  • Re: Linq casting and marshalling problems

    11-08-2007, 9:58 PM
    • Loading...
    • BrianMinister
    • Joined on 10-19-2007, 8:49 PM
    • Orange County, CA
    • Posts 13

    I have found my solutions and will be posting quite a bit on DLinq very soon.

    There is a whole lot that I need to dig into, so I can see all of the under pinnings.

    初めは容易である-継続は堅い。
    Beginning is easy - Continuing is hard.
    http://BriansCodeExtreme.blogspot.com
Page 1 of 1 (10 items)
Microsoft Communities
Page view counter