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. C#
  4. Passing Linq query results to a different function

Passing Linq query results to a different function

Scheduled Pinned Locked Moved C#
linqcsharpdatabasehelpquestion
16 Posts 4 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.
  • B brunoseixas

    convert the linq result to list

    var results = (from t in query
    where t.ProjectID == new Guid(this.Session["ProjectID"].ToString())
    orderby t.Number ascending
    select new
    {
    ID = t.ID,
    Tag = t.Number,
    Description = t.Description,
    SubSystems = GeneralHelper.TagSubSystems(t),
    Module = t.Area.Module.Name,
    Discipline = t.Discipline.Name,
    ITRs = GeneralHelper.TagITRsHTML(t)
    }).ToList();

    S Offline
    S Offline
    Swiftain
    wrote on last edited by
    #3

    If I convert it to list, then how do I define the function that takes the query result?

    N 1 Reply Last reply
    0
    • S Swiftain

      If I convert it to list, then how do I define the function that takes the query result?

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #4

      public void BindGridData(IEnumerable result)
      {
      gridView.DataSource = result;
      gridView.DataBind();
      }

      Also note the use of the pre tags to properly format the code snippet


      I know the language. I've read a book. - _Madmatt

      S 1 Reply Last reply
      0
      • N Not Active

        public void BindGridData(IEnumerable result)
        {
        gridView.DataSource = result;
        gridView.DataBind();
        }

        Also note the use of the pre tags to properly format the code snippet


        I know the language. I've read a book. - _Madmatt

        S Offline
        S Offline
        Swiftain
        wrote on last edited by
        #5

        IEnumerable requires an argument, IEnumerable result any idea what argument would work?

        I N 2 Replies Last reply
        0
        • S Swiftain

          IEnumerable requires an argument, IEnumerable result any idea what argument would work?

          I Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #6

          Create a structure to represent your query results, instead of using anonymous types. Then you know what kind of IEnumerable it is. Alternatively, make your Bind function generic, like so:

          public void BindGridData<T>(IEnumerable<T> result)
          {
          gridView.DataSource = result;
          gridView.DataBind();
          }

          Proud to have finally moved to the A-Ark. Which one are you in?
          Author of the Guardians Saga (Sci-Fi/Fantasy novels)

          S N 3 Replies Last reply
          0
          • I Ian Shlasko

            Create a structure to represent your query results, instead of using anonymous types. Then you know what kind of IEnumerable it is. Alternatively, make your Bind function generic, like so:

            public void BindGridData<T>(IEnumerable<T> result)
            {
            gridView.DataSource = result;
            gridView.DataBind();
            }

            Proud to have finally moved to the A-Ark. Which one are you in?
            Author of the Guardians Saga (Sci-Fi/Fantasy novels)

            S Offline
            S Offline
            Swiftain
            wrote on last edited by
            #7

            That worked! Thanks.

            1 Reply Last reply
            0
            • S Swiftain

              IEnumerable requires an argument, IEnumerable result any idea what argument would work?

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #8

              IEnumerable<T> requires a type but IEnumerable does not.


              I know the language. I've read a book. - _Madmatt

              modified on Tuesday, August 31, 2010 12:12 PM

              S 1 Reply Last reply
              0
              • I Ian Shlasko

                Create a structure to represent your query results, instead of using anonymous types. Then you know what kind of IEnumerable it is. Alternatively, make your Bind function generic, like so:

                public void BindGridData<T>(IEnumerable<T> result)
                {
                gridView.DataSource = result;
                gridView.DataBind();
                }

                Proud to have finally moved to the A-Ark. Which one are you in?
                Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #9

                Although I agree a concrete type is better for readability and good design it isn't necessary in order to pass the query to a method.


                I know the language. I've read a book. - _Madmatt

                S 1 Reply Last reply
                0
                • N Not Active

                  Although I agree a concrete type is better for readability and good design it isn't necessary in order to pass the query to a method.


                  I know the language. I've read a book. - _Madmatt

                  S Offline
                  S Offline
                  Swiftain
                  wrote on last edited by
                  #10

                  the reason i didnt define a concrete type is because the function will be accepting queries from different classes

                  N I 2 Replies Last reply
                  0
                  • N Not Active

                    IEnumerable<T> requires a type but IEnumerable does not.


                    I know the language. I've read a book. - _Madmatt

                    modified on Tuesday, August 31, 2010 12:12 PM

                    S Offline
                    S Offline
                    Swiftain
                    wrote on last edited by
                    #11

                    ???

                    N 1 Reply Last reply
                    0
                    • S Swiftain

                      ???

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #12

                      Sorry, the brackets didn't render. Fix it


                      I know the language. I've read a book. - _Madmatt

                      1 Reply Last reply
                      0
                      • S Swiftain

                        the reason i didnt define a concrete type is because the function will be accepting queries from different classes

                        I Offline
                        I Offline
                        Ian Shlasko
                        wrote on last edited by
                        #13

                        Ok, then the second solution is best :) Oh, and you don't need to do a ToList() if you're going that route. Linq results are all IEnumerable.

                        Proud to have finally moved to the A-Ark. Which one are you in?
                        Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                        1 Reply Last reply
                        0
                        • S Swiftain

                          the reason i didnt define a concrete type is because the function will be accepting queries from different classes

                          N Offline
                          N Offline
                          Not Active
                          wrote on last edited by
                          #14

                          Using generics, as Ian has suggested, doesn't preclude you from doing that. You would just need to create the class/struct for each query, which from a architectural perspective isn't a bad idea. However, if you have many different queries that are possible then perhaps you need to improve your design, one size fits all implementation are very difficult to maintain.


                          I know the language. I've read a book. - _Madmatt

                          1 Reply Last reply
                          0
                          • I Ian Shlasko

                            Create a structure to represent your query results, instead of using anonymous types. Then you know what kind of IEnumerable it is. Alternatively, make your Bind function generic, like so:

                            public void BindGridData<T>(IEnumerable<T> result)
                            {
                            gridView.DataSource = result;
                            gridView.DataBind();
                            }

                            Proud to have finally moved to the A-Ark. Which one are you in?
                            Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                            S Offline
                            S Offline
                            Swiftain
                            wrote on last edited by
                            #15

                            One more thing, lets say I store "result" in session variable like this public void BindGridData(IEnumerable result) { Session["result"] = result; gridView.DataSource = result; gridView.DataBind(); } and then I want to call this function within another event, how do I call it? how do I cast Session["result"]? Thanks

                            N 1 Reply Last reply
                            0
                            • S Swiftain

                              One more thing, lets say I store "result" in session variable like this public void BindGridData(IEnumerable result) { Session["result"] = result; gridView.DataSource = result; gridView.DataBind(); } and then I want to call this function within another event, how do I call it? how do I cast Session["result"]? Thanks

                              N Offline
                              N Offline
                              Not Active
                              wrote on last edited by
                              #16

                              First of all don't store it in session state. Remember this is the result of your query, not the query itself, and could be very large. It's also quite clear how to cast it. It is being passed into your method as an IEnumerable, guess what it should be cast as?


                              I know the language. I've read a book. - _Madmatt

                              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