Link to home
Start Free TrialLog in
Avatar of kevp75
kevp75Flag for United States of America

asked on

Linq To Array Q...

Ok.   I am trying desperately to get distinct values from an array.

So far...  unsuccessfully.

Right now, I need to show
VW
Audi

but it shows all values...  how can I select distinct values from this?  here's the code with the example array
Dim tmp = New String() {"VW|Jetta", "VW|Golf", "Audi|TT", "Audi|A4", "VW|Passat", "VW|Beetle"}
        Dim _Qry = From n In tmp.Distinct
        For Each item In _Qry
            Response.Write(item & "<br />")
        Next

Open in new window

Avatar of wdosanjos
wdosanjos
Flag of United States of America image

I tried your code and it works, that is, it only lists the distinct values.  It happens that all values in your array are distinct.  What's the result you expect?

The following code only displays the 6 distinct ones:

Dim tmp = New String() {"VW|Jetta", "VW|Golf", "Audi|TT", "Audi|A4", "VW|Passat", "VW|Beetle", "VW|Jetta", "VW|Golf", "Audi|TT", "Audi|A4", "VW|Passat", "VW|Beetle"}
Dim _Qry = From n In tmp.Distinct
	For Each item In _Qry
		Response.Write(item & "<br />")
	Next

Open in new window


Result:

VW|Jetta<br />
VW|Golf<br />
Audi|TT<br />
Audi|A4<br />
VW|Passat<br />
VW|Beetle<br />
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
P.S.

You don't really need the "Model =" part of the Select. "Select Key" should accomplish the same.
Avatar of kevp75

ASKER

exaclty what I was aiming at.
        Dim _Qry = (From n In tmp _
            Group n By Key = n.Substring(0, n.IndexOf("|")) Into Group _
            Select Key).Distinct

Gets me back
VW
Audi

thank yo9u very much!
Avatar of kevp75

ASKER

so....   how would I get the other side of the |?

I tried n.Substring(1, n.IndexOf("|"))  but of course, all that did was get rid of the first letter...  LOL

Avatar of kevp75

ASKER

ok.   never mind :)   n.Substring(n.IndexOf("|")) does it, I just have to replace the | with an empty string.   Thanks!
>>   n.Substring(n.IndexOf("|")) does it, I just have to replace the | with an empty string.   Thanks!

If you do:

 n.Substring(n.IndexOf("|") + 1) does it, I just have to replace the | with an empty string.   Thanks!

Open in new window


then you won't have to do the replacement   = )
*Sheesh*  copy and paste....

Take out the extraneous text of course!
Avatar of kevp75

ASKER

yea...figred that out a second after I posted that LOL