Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 780963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:09:26+00:00 2026-05-14T20:09:26+00:00

How can I do this better (so it actually works : ) I have

  • 0

How can I do this better (so it actually works : )

I have a LINQ Query that includes an order by that is based on a user preference. The user can decide if they would like the results ordered asc or desc.

If fuserclass.SortOrder = "Ascending" Then
   Dim mydat = (From c In dc.ITRS Order By c.Date Ascending Select c)
Else
   Dim mydat = (From c In dc.ITRS Order By c.Date Descending Select c)
End If

For each mydata in mydat ***<<<error "mydat is not declared"***

I know I could put my For Each loop inside the If and Else, but that seems silly to have the same code twice. I know you know of a better way : )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:09:27+00:00Added an answer on May 14, 2026 at 8:09 pm

    Use an extension method, and function-based LINQ (my VB is rusty as hell)

    VB.NET:

    <ExtensionAttribute> _
    Public Shared Function OrderByString(Of TSource, TKey) ( _
    source As IEnumerable(Of TSource), _
    keySelector As Func(Of TSource, TKey) _
        strType As String) As IOrderedEnumerable(Of TSource)
    
        If strType = "Ascending" Then
            return source.OrderBy(keySelector)
        Else
            return source.OrderByDescending(keySelector)
        End If
    End Function
    

    C#.NET:

    public static IOrderedEnumerable<TSource> OrderByString(
        this IEnumerable<TSource> source, 
        Func<TSource, TKey> keySelector,
        string strType)
    {
        if (strType == "Ascending")
            return source.OrderBy(keySelector);
        return source.OrderByDescending(keySelector);
    }
    

    Then call your query like this:

    Dim mydat = dc.ITRS.OrderByString(Function(item) item.Date, fuserclass.SortOrder)
    

    Or on C#:

    var mydat = dv.ITRS.OrderbyString(item => item.Date, fuserclass.SortOrder);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I rewrite this query to Doctrine DBAL query or better to createQuery()
I have a UDP server that reflects every ping message it receives (this works
Just wondering if anyone can optimize this usortMonths() function to be better? Basically I
I am a Ruby newbie. How can I write better for this function? can
This may seem a bit crazy, but if you can tell me a better
I ended up with this horrible code below, I can't get a better result
There's this ... MSDN page, IQueryable(Of T) Interface . Can you do a better
Can this MS-Access query be improved? i.e. the SELECT TOP 1 col FROM Table2
I have a page that is using tables, in FF etc it works perfect,
Any advice on how to fix this issue I have, or a better implementation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.