[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

linq to sql vb.net case statements in select and order by

Posted on 2009-07-09
9
Medium Priority
?
2,181 Views
Last Modified: 2013-11-11
Hi all. I am trying to use linq to do a case statement in the order by but the problem is i have a custom column names that i want to sort on.  The only way i could get it to work was by writing another statement to sort the x variable into a new variable of z then i use that to bind to a dropdownlist as it is sorted as i want. But surely there is a better way of doing this than with 2 statements!

Thanks in advance for any help...


Dim x = From y In db.TicketSources Join pu In  db.tbO2PortalUsers On y.O2PortalUserID Equals pu.fdID _
                   Select New With {.value = y.TicketSourceID, .Text = If(y.O2PortalUserID <> 0 And pu.Active = 1, pu.FirstName & " " & pu.LastName, y.SourceName)} 
 
Dim z = From s In x Order By s.Text Ascending
 
 
return z.toList()  'the sorted list

Open in new window

0
Comment
Question by:philipeharris
[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
  • 3
  • 3
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24819540
I know that you can use extension methods to sort, like in this article:

Custom sorting order in LINQ (ORDER BY WEIGHTING)
http://www.daniel-skinner.co.uk/custom-sorting-order-in-linq-order-by-weighting/18/03/2009

But, I don't know the VB.NET-equivalent syntax for that kind of extension method.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24821193
I am not sure about the ASP.NET dropdownlist but winforms version has a Sorted property.
0
 

Author Comment

by:philipeharris
ID: 24821426
Thanks both for you comments.

TheLearnedOne: i had seen those sorts during my extensive google searches, but i was hoping i could write the cope all in one statement like you can in actual SQL. Anyhow thanks for your reply.

CodeCruiser: No such property that i can find in asp.net VB version anyhow but again thankyou for your reply
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 24840094
I was able to sort a simple Select with an "Order By" clause:


        Dim db As New AdventureWorksDataContext()
 
        Dim q = From x In db.Products _
            Where x.Name.StartsWith("Road-650") _
            Order By x.Name _
            Select x
 
        Dim sb As New StringBuilder()
 
        For Each product As Product In q
            sb.Append(product.Name & vbCr & vbLf)
        Next

Open in new window

0
 

Author Comment

by:philipeharris
ID: 24847576
Thanks thelearnerone but that is an order by on a tables column name as apposed to what i was trying which was on a custom column name.  The order by for what i need would be the same as the select but its not possible as far as i can make out.

Thanks for your help.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24849186
Oh, yeah, custom column name.  I got lost in a context switch (d'oh).

I don't see a way to order by a custom column name, but that doesn't mean that there isn't a way, just that I don't know every subtle nuance of LINQ.
0
 

Accepted Solution

by:
philipeharris earned 0 total points
ID: 25096203
I dont think it should be deleted. If wanted it could be closed as nobody was able to give a better solution than that i gave in the question.
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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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