Computer
Computer

Reputation: 2227

Orderby after select clause in C#

I have a VB .Net query which im attempting to convert to C#. I thought i did it correctly but my attempt in C# results in no data returned where as the VB code does return data (both coming from the same data source)

Dim MyQuery = From s In MyContext.StudentPlacements _
                    Where (s.StudentLocation.URL = StudentUrl) _
                          Select s _
                         Order By s.Id Descending, MyContext.GenerateRandomId

C#

IQueryable<StudentPlacement> MyQuery = from s in MyContext.StudentPlacements
                            where (s.StudentLocation.URL == StudentUrl) 
                            orderby s.Id descending, MyContext.GenerateRandomId() 
                            select s;  

So i searched to see if i could add an orderby after the select in C# but all my attempts didnt seem to work. I dont think you can have an order by after the select in C#? I tried to change my C# code to use a dynamic variable but that didnt do the trick. Used an online converter which gave something different but it didnt compile.

Any idea how i could achieve the same code in C#?

Upvotes: 1

Views: 177

Answers (1)

Sotiris
Sotiris

Reputation: 52

The VB.Net does case insensitive comparisons for texts so the VB statement

s.StudentLocation.URL = StudentUrl

Returns different results from the same statement in C#

s.StudentLocation.URL == StudentUrl

Use

s.StudentLocation.URL.ToUpper() == StudentUrl.ToUpper()

OR

 s.StudentLocation.URL.Equals(StudentUrl,StringComparison.OrdinalIgnoreCase)

Upvotes: 1

Related Questions