This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Recently, I needed to develop a method that would take two parameters: one for the name of the Entity to query ("entityType") and one for the name of the column to return ("nameField"). I ended up writing something like this: public IList<LookupItem> GetLookupItems(Type entityType, string nameField) ... some more code here } } Note that due to the structure of the application I can not use generics here to pass in the type of the Entity. While this works fine, it did make me wonder if there would be some way of writing this using Linq. Does anybody know of any way to use variables in Linq queries? Also - vaguely related to this - is there a way to pass a Type object into a gereric parameter? E.g. something like: var myType = myObject.GetType(); var result = myMethod<myType>(string myString); ^^^ = variable Type? Thanks in advance for any assistance, Lucien.
|
|
|
The short answer is that no, this can't be done using LINQ, because the LINQ operators are strongly-typed and enforce compile-time checking on the kind of entity being queried. (This is a general feature of LINQ, and is not peculiar to LightSpeed's implementation. You'd run into the same issue using LINQ to Objects.) The long answer is that it can be done with LINQ, but involves such gargantuan contortions that it's only useful if you have no other way of composing the query you want -- and since you have the LightSpeed Find method, you do have another way. (See below if you want to see just how gargantuan the contortions are.) There is also a sample called Dynamic LINQ which allows you to write your LINQ queries and projections as strings (which you can build up at runtime) and it will translate them into expressions for you: see http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx, but I think this will still run into the problem of needing a compile-time-typed IQueryable to get started from. Regarding passing a variable type as a generic parameter, this isn't possible in the way you'd like it to be. You can use reflection to parameterise a generic method at runtime (MethodInfo.MakeGenericMethod) but because this parameterisation happens at runtime you can't interact with the method in a strongly-typed way. ----- Okay, as promised, here is how to perform a run-time projection using LINQ if you ever really really need to (cover your eyes if you are of a nervous disposition): private static IEnumerable<LookupItem> GetLookupItems(IUnitOfWork unitOfWork, Type t, string nameField) Usage: public void Test() public class LookupItem As you can see, much easier to use IUnitOfWork.Project instead! |
|
|
Hi Ivan, Good to "meet" you again! Wow - I'm impressed with your "long" solution and you taking the time to come up with that! I have to agree though, using Project is a lot easier - and it works. It was the conclusion I came to as well, after trying different scenarios, but it's good to have it confirmed by an expert. :-) Thanks again, Lucien
|
|