Solved

LINQ query to select distinct values osrted in ascending order

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

623 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