Solved

LINQ query to select distinct values osrted in ascending order

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

22 Experts available now in Live!

Get 1:1 Help Now