From: Rob on
When selecting records by forcing one field to be unique, is it
necessary to use the .Distinct(...) function with an external
comparator?

EX: Say you had a list of purchase records...multifields. You want to
get -just- a list of people who had bought items. The 'purchaser'
field must be unique, so you don't just get back the entire list, but
you want to retrieve all fields in the record, so you can't just use
..Distinct(). (with no args)

I've seen examples that require coding an external class, creating an
instance, then passing that object to .Distinct(...) but this seems
unduly complex. Is there a simpler way? I have Calvert's book, but
that doesn't seem to be covered. I thought that this would be built
in.
From: -- on
On Sat, 06 Mar 2010 19:58:03 -0500, Rob <Rob(a)no_mail_.com> wrote:

>When selecting records by forcing one field to be unique, is it
>necessary to use the .Distinct(...) function with an external
>comparator?
>
>EX: Say you had a list of purchase records...multifields. You want to
>get -just- a list of people who had bought items. The 'purchaser'
>field must be unique, so you don't just get back the entire list, but
>you want to retrieve all fields in the record, so you can't just use
>.Distinct(). (with no args)
>
>I've seen examples that require coding an external class, creating an
>instance, then passing that object to .Distinct(...) but this seems
>unduly complex. Is there a simpler way? I have Calvert's book, but
>that doesn't seem to be covered. I thought that this would be built
>in.

PS: I couldn't find any newsgroups containing the word "Linq", so
please advise if there are better places to post about Linq queries.
There did seem to be some here who are very knowledgable about it.
From: Peter Duniho on
Rob wrote:
> When selecting records by forcing one field to be unique, is it
> necessary to use the .Distinct(...) function with an external
> comparator?
>
> EX: Say you had a list of purchase records...multifields. You want to
> get -just- a list of people who had bought items. The 'purchaser'
> field must be unique, so you don't just get back the entire list, but
> you want to retrieve all fields in the record, so you can't just use
> ..Distinct(). (with no args) [...]

Your question is kind of vague, but it seems to me that if "you want to
retrieve all the fields in the record", thus implying that each row
(item) in the query of purchase records is in fact useful information,
not just the "purchaser" column (property) of those rows, then perhaps
what you really want is to use the Enumerable.GroupBy() method, grouping
by the "purchaser" value.

If you can describe more specifically the input you have and the exact
output you want, I or someone else can probably suggest the intermediate
code to achieve that.

Pete
From: Mr. Arnold on
Rob wrote:
> When selecting records by forcing one field to be unique, is it
> necessary to use the .Distinct(...) function with an external
> comparator?
>
> EX: Say you had a list of purchase records...multifields. You want to
> get -just- a list of people who had bought items. The 'purchaser'
> field must be unique, so you don't just get back the entire list, but
> you want to retrieve all fields in the record, so you can't just use
> .Distinct(). (with no args)
>
> I've seen examples that require coding an external class, creating an
> instance, then passing that object to .Distinct(...) but this seems
> unduly complex. Is there a simpler way? I have Calvert's book, but
> that doesn't seem to be covered. I thought that this would be built
> in.

I don't know. If it were me, I might try to this.


var list = new List<T>();

var distinctlist= (from a in resultset.distinct() select
a.purchaser).tolist();

foreach(var distinct in distinctlist)
{
var dist = (from a in resultset.Where(a => a.purchaser == distinct
select a).first();

list.add(dist);
}
From: Rob on
On Sat, 06 Mar 2010 17:56:16 -0800, Peter Duniho
<no.peted.spam(a)no.nwlink.spam.com> wrote:

>Rob wrote:
>> When selecting records by forcing one field to be unique, is it
>> necessary to use the .Distinct(...) function with an external
>> comparator?
>>
>> EX: Say you had a list of purchase records...multifields. You want to
>> get -just- a list of people who had bought items. The 'purchaser'
>> field must be unique, so you don't just get back the entire list, but
>> you want to retrieve all fields in the record, so you can't just use
>> ..Distinct(). (with no args) [...]
>
>Your question is kind of vague, but it seems to me that if "you want to
>retrieve all the fields in the record", thus implying that each row
>(item) in the query of purchase records is in fact useful information,
>not just the "purchaser" column (property) of those rows, then perhaps
>what you really want is to use the Enumerable.GroupBy() method, grouping
>by the "purchaser" value.
>
>If you can describe more specifically the input you have and the exact
>output you want, I or someone else can probably suggest the intermediate
>code to achieve that.
>
>Pete

1 Joe Smith 501 Spruce St book
2 Cathy Jones 6 Pine St scissors
3 James Thomas 75 6th Ave incense
4 Cathy Jones 6 Pine St towels
5 Joe Smith 501 Spruce St lightbulbs
6 Cathy Jones 6 Pine St tablecloth

The desired end result is to take one 'sample' for each person, so
that would be something like:

1 Joe Smith 501 Spruce St book
2 Cathy Jones 6 Pine St scissors
3 James Thomas 75 6th Ave incense

Given that there are multiple fields, I can't just say .Distinct().
I'd have to provide an external function to specifically compare
names. I thought it likely that there was a built-in function that
would handle this right inside the Linq query, but maybe not.

BTW, the source data is not normalized. It is a flat source, similar
to the above.
 |  Next  |  Last
Pages: 1 2
Prev: VB6 OCX In C# Windows App
Next: xml and xml schema