Solved

Linq To Array Q...

Posted on 2011-03-17
9
606 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:kevp75
  • 4
  • 4
9 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35157373
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 />
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35157395
You already have "distinct" values in your list because each of those strings are different from each other. What you are trying to do is get "distinctness" based on some substring.

Try this modification:
Dim tmp = New String() {"VW|Jetta", "VW|Golf", "Audi|TT", "Audi|A4", "VW|Passat", "VW|Beetle"}
Dim _Qry = (From n In tmp _
            Group n By Key = n.Substring(0, n.IndexOf("|")) Into Group _
            Select Model = Key).Distinct

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35157414
P.S.

You don't really need the "Model =" part of the Select. "Select Key" should accomplish the same.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 25

Author Comment

by:kevp75
ID: 35157482
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!
0
 
LVL 25

Author Comment

by:kevp75
ID: 35157503
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

0
 
LVL 25

Author Comment

by:kevp75
ID: 35157561
ok.   never mind :)   n.Substring(n.IndexOf("|")) does it, I just have to replace the | with an empty string.   Thanks!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35157598
>>   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   = )
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35157614
*Sheesh*  copy and paste....

Take out the extraneous text of course!
0
 
LVL 25

Author Comment

by:kevp75
ID: 35157792
yea...figred that out a second after I posted that LOL
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question