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 in Function

Linq in Function

Scheduled Pinned Locked Moved LINQ
databasequestioncsharplinq
7 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.
  • D Offline
    D Offline
    dkosasih
    wrote on last edited by
    #1

    Hi all, I just started to learn about linq and have some question. say normally we would query something like this bt01DataContext db = new bt01DataContext("Data Source=SOURCE;Initial Catalog=XXX;User ID=XX;Password=XX;"); var query = from b in db.CATEGORies select b; foreach (var cat in query) { ... } now, I want split it into one separate class so that i will have the query all together in 1 file say Query.cs. public DATA TYPE** GetAllCategories() { try { var query = from b in db.CATEGORies select b; return query; } catch (Exception ex) { throw ex; } } in my code I want to call something like this Query q = new Query; var sampleQuery = q.GetAllCategories(); question: Do I have to use something else to replace the var? and what return DATA TYPE** should I use in order to get the result I want? i read from somewhere that i could use bt01(I have bt01.dbml) as data type but I couldn't get it to work when I try that.. any information would be greatly appreciated... thx guys

    I D 2 Replies Last reply
    0
    • D dkosasih

      Hi all, I just started to learn about linq and have some question. say normally we would query something like this bt01DataContext db = new bt01DataContext("Data Source=SOURCE;Initial Catalog=XXX;User ID=XX;Password=XX;"); var query = from b in db.CATEGORies select b; foreach (var cat in query) { ... } now, I want split it into one separate class so that i will have the query all together in 1 file say Query.cs. public DATA TYPE** GetAllCategories() { try { var query = from b in db.CATEGORies select b; return query; } catch (Exception ex) { throw ex; } } in my code I want to call something like this Query q = new Query; var sampleQuery = q.GetAllCategories(); question: Do I have to use something else to replace the var? and what return DATA TYPE** should I use in order to get the result I want? i read from somewhere that i could use bt01(I have bt01.dbml) as data type but I couldn't get it to work when I try that.. any information would be greatly appreciated... thx guys

      I Offline
      I Offline
      Ian McCaul
      wrote on last edited by
      #2

      The return type I believe would be IEnumerable<Category>. You dont need to replace var.

      H J 2 Replies Last reply
      0
      • I Ian McCaul

        The return type I believe would be IEnumerable<Category>. You dont need to replace var.

        H Offline
        H Offline
        hnguyenec
        wrote on last edited by
        #3

        Thank you so much... i works like a charm. :)

        D 1 Reply Last reply
        0
        • H hnguyenec

          Thank you so much... i works like a charm. :)

          D Offline
          D Offline
          dkosasih
          wrote on last edited by
          #4

          indeed... thank you

          1 Reply Last reply
          0
          • D dkosasih

            Hi all, I just started to learn about linq and have some question. say normally we would query something like this bt01DataContext db = new bt01DataContext("Data Source=SOURCE;Initial Catalog=XXX;User ID=XX;Password=XX;"); var query = from b in db.CATEGORies select b; foreach (var cat in query) { ... } now, I want split it into one separate class so that i will have the query all together in 1 file say Query.cs. public DATA TYPE** GetAllCategories() { try { var query = from b in db.CATEGORies select b; return query; } catch (Exception ex) { throw ex; } } in my code I want to call something like this Query q = new Query; var sampleQuery = q.GetAllCategories(); question: Do I have to use something else to replace the var? and what return DATA TYPE** should I use in order to get the result I want? i read from somewhere that i could use bt01(I have bt01.dbml) as data type but I couldn't get it to work when I try that.. any information would be greatly appreciated... thx guys

            D Offline
            D Offline
            dkosasih
            wrote on last edited by
            #5

            I got one more question that I think I have missed out. in this code: var query = from b in db.CATEGORies select b; foreach (var cat in query) { ... } in the code "var cat", what is the data type of that var if i need to pass that value to other function? again, thank you very much.

            I 1 Reply Last reply
            0
            • D dkosasih

              I got one more question that I think I have missed out. in this code: var query = from b in db.CATEGORies select b; foreach (var cat in query) { ... } in the code "var cat", what is the data type of that var if i need to pass that value to other function? again, thank you very much.

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

              Well the query will return a IEnumerable<Category> type so in a foreach the cat type is Category.

              1 Reply Last reply
              0
              • I Ian McCaul

                The return type I believe would be IEnumerable<Category>. You dont need to replace var.

                J Offline
                J Offline
                jggimenez
                wrote on last edited by
                #7

                The return type would actually be IQueryable<Category> but as it is based on IEnumerable<Category> they are "interchangeable" (depending on you needs of course).

                ...Of course, we're talking about software development here, where the simplest solution is often ignored..

                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