LINQ Distinct question

R

Ron

Hi,

i have been lokking for a resolution for this seemingly simple problem I'm
having with no success.

I have a Generic List of CompanyPayrollTaxes:
List<CompanyPayrolltax>m_CompanyPayrollTaxRecordsToPay

I need to get a sub collection of each CompanyPayrolltax by Distinct
PaymentNumber

My problem is that I have been using:
foreach(CompanypayrollTax t in
m_CompanyPayrollTaxRecordsToPay.FindAll((CompanyPayrollTax pt) =>
pt.PaymentNumber).Distinct)

and it doesn't seem to have any effect whatsoever.

What am I missing?

Thanks!
Ron
 
C

Cowboy \(Gregory A. Beamer\)

I apologize for not being directly specific and solving the problem, but I
don't have the time to rip through the LINQ and a cursory glance did not
fire enough synapses for me to come up with the answer.

For a good blog entry on distinct, I would try this one:
http://blogs.msdn.com/charlie/archive/2006/11/19/linq-farm-group-and-distinct.aspx

Many times, with LINQ, you have to create your "table definition" prior to
adding a filter. David Yack displays this in his post in this thread:
http://forums.asp.net/t/1220691.aspx

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

*************************************************
| Think outside the box! |
*************************************************
 
P

Pavel Minaev

My problem is that I have been using:
foreach(CompanypayrollTax t in
m_CompanyPayrollTaxRecordsToPay.FindAll((CompanyPayrollTax pt) =>
pt.PaymentNumber).Distinct)

and it doesn't seem to have any effect whatsoever.

The above code shouldn't even compile. First of all, List<T>.FindAll()
takes a predicate. Unless pt.PaymentNumber is bool (which it doesn't
seem to be, judging from its name), than your lambda does not return
bool, and therefore it will not match the signature of Predicate<T>.
Then there is a fact that Distinct() should be a method call, and you
miss the parentheses there - which would make you reference a method
group instead, and that should also produce a compile-time error. This
makes the whole statement meaningless.

Please, either post the actual code that compiles, or explain better
what you're trying to do. Reading this:
I need to get a sub collection of each CompanyPayrolltax by Distinct PaymentNumber

I think that you might want to look into Enumerable.GroupBy(). If
that's not relevant to your case, then I'm totally lost at what you
actually want to do here.
 
R

Ron

Peter,

Thank you for the explanation. Yes I did rush and prepared only some psuedo
code to illustrate what I was trying to do. If I got the code to compile I
wouldn't have had the question in the first place I suppose :)

The Group By worked well...thank you!

Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Foreach Loop. Distinct 8
constains or linq 5
Linq 1
LINQ to XML 2
LINQ and Distinct method and custom IEqualityComparer 1
Using LINQ to Initialise a List 7
Linq. String 5
Linq. Aggregate 4

Top