Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. LINQ orderby issue

LINQ orderby issue

Scheduled Pinned Locked Moved LINQ
helpcsharpdatabaselinqtutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Illegal Operation
    wrote on last edited by
    #1

    Hi, I have the following code:

            var query = from a in ds.Tables\[0\].AsEnumerable()
                        from b in ds.Tables\[2\].AsEnumerable()
                        where a.Field<string>("CaseId") == txtCaseID.Text && a.Field<string>("Password") == txtPassword.Text
                        where a.Field<int>("Case\_Id") == b.Field<int>("Notes\_Id")
                        orderby b.Field<string>("NoteCreated") descending
                        select new
                        {
                            NoteCreated = b.Field<string>("NoteCreated"),
                            NoteContent = b.Field<string>("NoteContent"),
                        };
    
            gv\_CaseDetails.DataSource = query.ToList();
            gv\_CaseDetails.DataBind();
    

    I need to order the results descending as can be seen, the problem is that the NoteCreated value is a Date but it's passed as a string. Now if I orderby NoteCreated descending it orders the results using only the first 2 digits. Example: The NoteCreated format is 30/01/2008. The above code will orderby the 30 only. Can someone please tell me how I can convert/cast the value to datetime so that I can use it? Thank you!!

    Illegal Operation

    T J 2 Replies Last reply
    0
    • I Illegal Operation

      Hi, I have the following code:

              var query = from a in ds.Tables\[0\].AsEnumerable()
                          from b in ds.Tables\[2\].AsEnumerable()
                          where a.Field<string>("CaseId") == txtCaseID.Text && a.Field<string>("Password") == txtPassword.Text
                          where a.Field<int>("Case\_Id") == b.Field<int>("Notes\_Id")
                          orderby b.Field<string>("NoteCreated") descending
                          select new
                          {
                              NoteCreated = b.Field<string>("NoteCreated"),
                              NoteContent = b.Field<string>("NoteContent"),
                          };
      
              gv\_CaseDetails.DataSource = query.ToList();
              gv\_CaseDetails.DataBind();
      

      I need to order the results descending as can be seen, the problem is that the NoteCreated value is a Date but it's passed as a string. Now if I orderby NoteCreated descending it orders the results using only the first 2 digits. Example: The NoteCreated format is 30/01/2008. The above code will orderby the 30 only. Can someone please tell me how I can convert/cast the value to datetime so that I can use it? Thank you!!

      Illegal Operation

      T Offline
      T Offline
      TylerBrinks
      wrote on last edited by
      #2

      You can use the "let" keyword in linq to set a variable to a DateTime value and then do your ordering on that variable.

      var query ....
      where ...
      let D =
      ...
      orderby D
      ...

      1 Reply Last reply
      0
      • I Illegal Operation

        Hi, I have the following code:

                var query = from a in ds.Tables\[0\].AsEnumerable()
                            from b in ds.Tables\[2\].AsEnumerable()
                            where a.Field<string>("CaseId") == txtCaseID.Text && a.Field<string>("Password") == txtPassword.Text
                            where a.Field<int>("Case\_Id") == b.Field<int>("Notes\_Id")
                            orderby b.Field<string>("NoteCreated") descending
                            select new
                            {
                                NoteCreated = b.Field<string>("NoteCreated"),
                                NoteContent = b.Field<string>("NoteContent"),
                            };
        
                gv\_CaseDetails.DataSource = query.ToList();
                gv\_CaseDetails.DataBind();
        

        I need to order the results descending as can be seen, the problem is that the NoteCreated value is a Date but it's passed as a string. Now if I orderby NoteCreated descending it orders the results using only the first 2 digits. Example: The NoteCreated format is 30/01/2008. The above code will orderby the 30 only. Can someone please tell me how I can convert/cast the value to datetime so that I can use it? Thank you!!

        Illegal Operation

        J Offline
        J Offline
        Jon Rista
        wrote on last edited by
        #3

        var query = from a in ds.Tables[0].AsEnumerable()
        from b in ds.Tables[2].AsEnumerable()
        where a.Field("CaseId") == txtCaseID.Text && a.Field("Password") == txtPassword.Text
        where a.Field("Case_Id") == b.Field("Notes_Id")
        orderby Convert.ToDateTime(b.Field("NoteCreated")) descending
        select new
        {
        NoteCreated = b.Field("NoteCreated"),
        NoteContent = b.Field("NoteContent"),
        };

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups