Solved

Linq To Array Q...

Posted on 2011-03-17
9
628 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

718 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