Solved

LINQ query to select distinct values osrted in ascending order

Posted on 2012-04-10
4
804 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
[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 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
These days socially coordinated efforts have turned into a critical requirement for enterprises.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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