Solved

Linq To Array Q...

Posted on 2011-03-17
9
587 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 74

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 74

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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 74

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 74

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now