Linq object Enumeration?

Who Else Wants To Learn Easy, Amazing, Magic Tricks Exposed. Effects & Illusions That Will Turn You Into A Magic Mogul With Your Friends & Family, Almost Overnight? Get it now!

As others have said, it sounds like you want to be iterating over the values, not the key/value pairs That means you could use a query like this: var results = from acctSum value in acct. Values where acctSum. Diff!

= 0 select acctSum but personally I wouldn't bother using a query expression in this case. I'd just use the extension methods: var results = acct.Values. Where(acctSum => acctSum.

Diff! = 0) Now, in terms of the return type, I strongly suspect you want to return IEnumerable Discrepancies() { return acct.Values. Where(acctSum => acctSum.

Diff! = 0); } or you might want to "materialize" the query - get all the results in one go, and return those (rather than returning a query which can be iterated over lazily). For example: public IEnumerable Discrepancies() { return acct.Values.

Where(acctSum => acctSum. Diff! = 0).ToList(); } In particular, by materializing the query you're allowing callers to modify acct while they're iterating over these results - if they try to do so while they're iterating over the raw query, they'll get an exception You'll also need to change your AcctSum class, because ArrayList isn't a generic type in .NET.

Did you mean to use List.

As others have said, it sounds like you want to be iterating over the values, not the key/value pairs. That means you could use a query like this: var results = from acctSum value in acct. Values where acctSum.

Diff! = 0 select acctSum; but personally I wouldn't bother using a query expression in this case. I'd just use the extension methods: var results = acct.Values.

Where(acctSum => acctSum. Diff! = 0); Now, in terms of the return type, I strongly suspect you want to return IEnumerable rather than IEnumerator or IEnumerator.So you might want: public IEnumerable Discrepancies() { return acct.Values.

Where(acctSum => acctSum. Diff! = 0); } or you might want to "materialize" the query - get all the results in one go, and return those (rather than returning a query which can be iterated over lazily).

For example: public IEnumerable Discrepancies() { return acct.Values. Where(acctSum => acctSum. Diff!

= 0).ToList(); } In particular, by materializing the query you're allowing callers to modify acct while they're iterating over these results - if they try to do so while they're iterating over the raw query, they'll get an exception. You'll also need to change your AcctSum class, because ArrayList isn't a generic type in .NET. Did you mean to use List?

I will switch the ArrayList to List, I saw it in another sample. I'm trying this out now, thanks! – kes Jan 11 at 13:20 the last example, .ToList(), was exactly what I was looking for; The whole thing still feels like a magic trick though.

I guess it's time to take of the blinders and learn the new paradigm :) – kes Jan 11 at 14:18.

The problem is that as you iterate over your dictionary, you are casting each item as an Acct, when in fact, each of the items in your Dictionary is a KeyValuePair. Try something like: from KeyValuePair acctKVP in acct // assuming acct is of type Dictionary where acctKvp.Value. Diff!

= 0.

When you iterate over acct, each item is a key/value pair from the dictionary. Assuming the Acct object is the value in each pair, you would get all of the nonzero accounts like this: from a in acct. Values where a.

Diff! = 0 select a.

Well, it's because acct is a dictionary (which is an IEnumerable, iirc, under the hood. Change your source from acct to acct.Values.

First, the return type should be IEnumerator, not IEnumerable. Secondly, acct contains a collection of key value pairs, so you have to run the query on the key value (in this case) void Main() { var acctInfo = new AcctInfo(); acctInfo.Discrepancies(); } public class AcctSum { string ID; Decimal Amt1; Decimal Amt2; public Decimal Diff; ArrayList lines; } public class AcctInfo { public IEnumerator Discrepancies() { var acct = new Dictionary {{1, new AcctSum() {Diff = 0.0M}}, {2, new AcctSum() {Diff = 1.0M}}}; var results = (from AcctSum a in acct. Values where a.

Diff! = 0 select a).AsEnumerable(); foreach (var result in results)//at runtime this generates an error { } return results.GetEnumerator(); } }.

Actually, I would return IEnumerable instead of IEnumerator. It's pretty rare to return an iterator from anything other than a GetEnumerator method implementing IEnumerable or IEnumerable. – Jon Skeet Jan 11 at 6:26 As would I, but as he was returning the GetEnumerator(), I figured it was deliberate.

Either way, something had to change :) – Rob Jan 11 at 6:29 yup, I messed up. I was trying to implement so that I could do foreach and/or return it for datagrid binding. And yes, for binding I needed to adjust the variables to properties :) – kes Jan 11 at 21:34.

Linq namespace is in the System. Core assembly (in System.Core. The Enumerable class contains LINQ standard query operators that operate on objects that implement IEnumerable.

The Queryable class contains LINQ standard query operators that operate on objects that implement IQueryable. For information about LINQ to SQL, see LINQ to SQL.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions