Results 1 to 6 of 6

Thread: Need help with typed dataset and LINQ

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Need help with typed dataset and LINQ

    Hi!

    I am trying to write the following as a lambda/linq expression:

    Code:
                var areaGroups = _areaManager.GetAreaGroupByName(areaGroupName).AREAGROUP;
    
                foreach (AreaGroupDS.AREAGROUPRow r in areaGroups.Rows)
                {
                    Debug.WriteLine(r.NAME);
                }
    I have included System.Data.DataSetExtensions, added all using statements I need but I can't convert the above statement to a lambda. The problem seems to be that there are no typed collections of the rows, just the untyped collection called "Rows", and I don't know how to cast that to the typed datarow type...? Or is the typed dataset C# code created incorrectly, it should expose a typed rowcollection if I am not mistaken?


    The above code works fine because it safely casts every item in the Rows collection to a upDS.AREAGROUPRow, but I can't do the same in LINQ... like

    Code:
    var query = from x in areaGroups.Rows
                            where x.

    Using .NET 3.5 and VS 2008

    HELP!

    /Henrik

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: Need help with typed dataset and LINQ

    Firstly, you need to distinguish between a LINQ query and a Lambda expression. They are not the same thing. Lambda expressions are a langauge feature that was introduced to support LINQ, but so are type inference, anonymous types and extension methods.

    One reason that you're confused is the fact that you're not actually trying to get a list as a result, which is what a query does. You're trying to perform an action. Here's what jumps out at me as the most concise way to do that:
    Code:
    Array.ForEach(areaGroups.AsEnumerable().ToArray(), row => Debug.WriteLine(row.NAME));
    The highlighted part is the Lambda, i.e. the bit that contains the '=>' operator.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: Need help with typed dataset and LINQ

    Quote Originally Posted by jmcilhinney View Post
    Firstly, you need to distinguish between a LINQ query and a Lambda expression. They are not the same thing. Lambda expressions are a langauge feature that was introduced to support LINQ, but so are type inference, anonymous types and extension methods.

    One reason that you're confused is the fact that you're not actually trying to get a list as a result, which is what a query does. You're trying to perform an action. Here's what jumps out at me as the most concise way to do that:
    Code:
    Array.ForEach(areaGroups.AsEnumerable().ToArray(), row => Debug.WriteLine(row.NAME));
    The highlighted part is the Lambda, i.e. the bit that contains the '=>' operator.
    Hi!

    Thanks for the heads up and corrections. You are right an all counds. So I guess what I really try to accomplish is to use "LINQ for Datasets" against a typed dataset. I get the typed dataset, but I am unable to query against it. As you can see in my example code, it should work by using

    Code:
    var res = from x in ds.TABLE1
                   where x.COL1 == 'Donald'
                  select x;
    The above query should give me a list of typed datarows, right? The problem is that I can't write that query, since the x.COL1 won't compile. I have googled lots of examples that suggests that the above code SHOULD work. But it does not in my case.

    The generated classes are in a dll which I reference, maybe they have been generated with an ancient version of MSDatasetGenerator?

    regards
    Henrik

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: Need help with typed dataset and LINQ

    Also, the code below works just fine;

    Code:
     var areaGroups = _areaManager.GetAreaGroupByName(areaGroupName).AREAGROUP.AsEnumerable();
    
                var isActive = areaGroups.First().Field<decimal>("ISACTIVE");
    But I dont't want to use AsEnumerable() and Field since it is a typed dataset... and from what I can read in the documentation around typed datasets, I should not have to...

    /Henrik

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: Need help with typed dataset and LINQ

    since the x.COL1 won't compile
    Why not? Presumably if it won't compile, it should at least be telling you why. The compiler doesn't simply say "mmm, nope, not going to do it." ... usually it's much more descriptive than that.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: Need help with typed dataset and LINQ

    Quote Originally Posted by techgnome View Post
    Why not? Presumably if it won't compile, it should at least be telling you why. The compiler doesn't simply say "mmm, nope, not going to do it." ... usually it's much more descriptive than that.


    -tg
    You are right, but since intellisense failed on me 8it is usually very clever, even at design time) I didn't bother to compile it. The error with this code

    Code:
     var areaGroups = _areaManager.GetAreaGroupByName(areaGroupName).AREAGROUP;
                var query = from x in areaGroups
                             select x.ISACTIVE;
    is

    "cannot convert lambda expression to type "string" because it is not a delegate type" with a red underline on "select".

    I ahve written linq for 2 years now, and it has never failed on this, which makes me want to blame the generated typed datasets somehow.

    http://msdn.microsoft.com/en-us/library/bb397927.aspx

    If I treat the AREAGROUP typed datatable as an untyped dattable (with AsEnumerable() and Fields) it works fine. But I want the type safety of typed datasets, and get rid of the magic strings used for the indexers.

    cheers
    Henrik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width