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
Solved

Linq To Array Q...

Posted on 2011-03-17
9
614 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
DevOps Toolchain Recommendations

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

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

828 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