Dynamic Select on columns?
-
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
-
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
-
You can use dynamic LINQ, e.g.,
var query = db.Customers
.Select("new (Name, Phone)");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
-
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
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