-
Apr 7th, 2014, 04:44 AM
#1
Thread Starter
Frenzied Member
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
-
Apr 7th, 2014, 05:22 AM
#2
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.
-
Apr 7th, 2014, 06:53 AM
#3
Thread Starter
Frenzied Member
Re: Need help with typed dataset and LINQ
Originally Posted by jmcilhinney
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
-
Apr 7th, 2014, 06:57 AM
#4
Thread Starter
Frenzied Member
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
-
Apr 7th, 2014, 09:34 AM
#5
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
-
Apr 8th, 2014, 12:56 AM
#6
Thread Starter
Frenzied Member
Re: Need help with typed dataset and LINQ
Originally Posted by techgnome
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|