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. Dynamic Select on columns?

Dynamic Select on columns?

Scheduled Pinned Locked Moved LINQ
csharpquestiondatabaselinqdata-structures
4 Posts 2 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.
  • E Offline
    E Offline
    Erik06se
    wrote on last edited by
    #1

    Hi, I am just starting with a project that will use Linq To Sql. What I want to do is to do a dynamic SELECT here. I have a result from a previous reading, that gave e.g. 'fieldName1' 'fieldName2' 'fieldName3' 'fieldName4' as strings in an array. What I want to do is a new SELECT with only those field: like 'SELECT fieldName, fieldName2, fieldName3, fieldName4 FROM myTAb Where My Id = 10' So, if the first question just had give me fieldName1, fieldName2 as result the next one should have been like :'SELECT fieldName, fieldName2FROM myTAb Where My Id = 10'. This is pretty easy in 'normal' sql with concatinated string, but I cannot find a way to do it in LingToSql. Do anyone have any answer? If you could give me an code-example as well (e.g. with Northwind) in c# I will be really happy. /Erik

    A 1 Reply Last reply
    0
    • E Erik06se

      Hi, I am just starting with a project that will use Linq To Sql. What I want to do is to do a dynamic SELECT here. I have a result from a previous reading, that gave e.g. 'fieldName1' 'fieldName2' 'fieldName3' 'fieldName4' as strings in an array. What I want to do is a new SELECT with only those field: like 'SELECT fieldName, fieldName2, fieldName3, fieldName4 FROM myTAb Where My Id = 10' So, if the first question just had give me fieldName1, fieldName2 as result the next one should have been like :'SELECT fieldName, fieldName2FROM myTAb Where My Id = 10'. This is pretty easy in 'normal' sql with concatinated string, but I cannot find a way to do it in LingToSql. Do anyone have any answer? If you could give me an code-example as well (e.g. with Northwind) in c# I will be really happy. /Erik

      A Offline
      A Offline
      ABitSmart
      wrote on last edited by
      #2

      You can use dynamic LINQ, e.g.,

      var query = db.Customers
      .Select("new (Name, Phone)");

      ScottGu's blog[^]

      E 1 Reply Last reply
      0
      • A ABitSmart

        You can use dynamic LINQ, e.g.,

        var query = db.Customers
        .Select("new (Name, Phone)");

        ScottGu's blog[^]

        E Offline
        E Offline
        Erik06se
        wrote on last edited by
        #3

        Hi. I have tried that, but the compile-message says: Error The type arguments for method 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. I am actually in my case using 'db' as a databasecontext, and I also tried to do it on the first query, with the same result. First I try your solution, and then I try like: I first do a question like var query = from tab1 in contextDB.TableCust where tab1.ID == _customer select tab1; and then: var subsetQuestion= from newtab in query .Select("new (Name, Phone)"); But with the same compile-error. Is there any other way to do it, or am misunderstanding anything here? /E

        A 1 Reply Last reply
        0
        • E Erik06se

          Hi. I have tried that, but the compile-message says: Error The type arguments for method 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. I am actually in my case using 'db' as a databasecontext, and I also tried to do it on the first query, with the same result. First I try your solution, and then I try like: I first do a question like var query = from tab1 in contextDB.TableCust where tab1.ID == _customer select tab1; and then: var subsetQuestion= from newtab in query .Select("new (Name, Phone)"); But with the same compile-error. Is there any other way to do it, or am misunderstanding anything here? /E

          A Offline
          A Offline
          ABitSmart
          wrote on last edited by
          #4

          Read Scott's post, Downloading the LINQ Dynamic Query Library section. This is a wrapper API. You will have to download the file(it's name is DynamicLibrary.cs present in the sample's folder), add it to your project and then add the namespace in your class. After doing this everything should work. Edit: As I said, the blog mentions wrappers. You can see the actual implementation in the class file or here[^]

          modified on Friday, February 27, 2009 11:22 PM

          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