Solved

LINQ query to select distinct values osrted in ascending order

Posted on 2012-04-10
4
797 Views
Last Modified: 2012-04-11
I am trying to build a LINQ to object query to retrieve distinct set of value sorted in ascending order, where li["Location_x0020__x0020_"]. != null

Here is what I have, which does not work correctly.

 var distLocations = (from ListItem li in allLocation.AsEnumerable()
                        select new { Location = li["Location_x0020__x0020_"].ToString() }).Distinct();
0
Comment
Question by:countrymeister
4 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37828883
What is the problem with the LINQ query you posted?  Does it throws an exception or simply doesn't provide the results you expected?
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 37828891
try this:

 var distLocations = (from ListItem li in allLocation.AsEnumerable()
                        orderby Location
                        select new { Location = li["Location_x0020__x0020_"].ToString() }).Distinct();

AW
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37829394
well i couldn't really understood your question, but here is what i have come up with assuming alllocation is a listbox.
I try to simulate the enviroment in my space and for the markup given below:-
<asp:ListBox id="lstValue" Width="100px" runat="server">
             <asp:ListItem>Item 1</asp:ListItem>
             <asp:ListItem>Item 2</asp:ListItem>
             <asp:ListItem>Item 3</asp:ListItem>
             <asp:ListItem Value="Value 4">Item 3</asp:ListItem>
             <asp:ListItem Text="Item 5" Value="Value 5" Selected="True"/>
             <asp:ListItem>Item 2</asp:ListItem>
         </asp:ListBox>

Open in new window


Here is the LINQ Query that will get the distinct listview items in ascending order:-
var items = lstValue.Items.Cast<ListItem>()
            .Select(item => item.Text)
            .Distinct()
            .OrderBy(s => s)
            .ToList();

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37829721
Try:

var distLocations = (from ListItem li in allLocation.AsEnumerable()
                     let value = li["Location_x0020__x0020_"] as string
                     where value != null
                     orderby value
                     select new { Location = value }).Distinct();

Open in new window

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

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