6

I'm using the Dynamic Linq Library (this one) in my .NET MVC application to query a SQL Server database. It's all working fine so far.

However, the Dynamic Linq Library gives an "Expression expected" error whenever I use square brackets to designate troublesome column names containing spaces or hyphens. Here are some examples:

var query = context.FetchIceCream().AsQueryable().Where("Chocolate = 1"); // This is fine
var query = context.FetchIceCream().AsQueryable().Where("[Rum and Raisin] = 1"); // This results in an "Expression expected" error

I'm not able to rename any column names, so that's not an option - I need to be able to sort this out in code. I've searched high and low for a solution to this but to no avail... please help to save my sanity!

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
Chris
  • 435
  • 3
  • 9
  • this is pure guesswork, so you can all but assume that it's wrong, but have you tried using single quotes around the name and see what happens? – Sam I am says Reinstate Monica Aug 08 '12 at 15:31
  • Thanks for the reply but I'm afraid it doesn't work - it results in a different error altogether ("Character literal must contain exactly one character", if you're interested!) – Chris Aug 08 '12 at 15:37
  • There has to be a cleaner answer so only a comment. Could you create a view with no spaces? – paparazzo Aug 08 '12 at 15:50
  • Not really - a lot of these column names are created dynamically, so we need the code to be able to handle new column names that might not already be known. – Chris Aug 08 '12 at 16:53
  • 2
    The name you use in the Where clause must be a property of the object which is in the IQueryable collection. So I don't understand what you are trying to do here. A property name can't have any spaces or hyphens. – Patrick Koorevaar Aug 08 '12 at 20:25
  • The problem is that the I am querying a recordset returned by a stored procedure which dynamically creates column names. In this case, there are indeed several column names returned which contain spaces or hyphens. – Chris Aug 09 '12 at 09:16
  • @Chris How have you been able to map this sproc to a function? I mean, there must be some (fixed) complex type to capture the results. – Gert Arnold Aug 10 '12 at 07:10

2 Answers2

2

So I think this is the answer then:
The name you use in the Where clause must be a property of the object which you have in the Queryable collection.

Patrick Koorevaar
  • 1,219
  • 15
  • 24
  • See my comment above - the Queryable collection is a recordset returned by a stored proc with dynamic column names, which don't already exist as objects. I'm able to successfully query all other dynamic columns in this manner, just not the ones that contain spaces or hyphens. – Chris Aug 09 '12 at 09:18
  • What underlying LINQ provider are you using? They names might be mangled by it in order to become valid C# property names. Try `RumandRaisin` or `Rum_and_Raisin`. – Allon Guralnek Aug 27 '12 at 08:52
0

I would advise against using Dynamic LINQ at all. It is possible to build up queries step by step by just adding things to the IQueryable<T>.

Jesper
  • 7,477
  • 4
  • 40
  • 57
  • Sadly that's not really an option - we can't really uproot the whole infrastructure of the project in this case, as time won't allow it. – Chris Aug 08 '12 at 16:48
  • In that case, Patrick Koorevaar's comment on the question is right. You can't reference a C# property named "Rum and Raisin" because there's no such C# property. Dynamic LINQ works on whatever LINQ provider you're using and you would have to had mapped that field to a valid property name in the process. It's this property name that you have to use. – Jesper Aug 09 '12 at 07:45