1

I have:

string table_name = "Orders";
string column_name = "Name";
string identity_column = "OrderID"
int identity_value = 5;

How can I achieve the following (dynamically):

var result = (from order in db.Orders
              where order.OrderId== identity_value 
              select order).SingleOrDefault();

For security reasons i cannot create a whole sql query and then execute it to server.

enb081
  • 3,831
  • 11
  • 43
  • 66
  • possible duplicate of [Dynamically generate LINQ queries](http://stackoverflow.com/questions/9505189/dynamically-generate-linq-queries) – 5uperdan Oct 14 '14 at 10:23
  • already checked that. i have a different scenario. – enb081 Oct 14 '14 at 10:28
  • ah yes, i can see what you mean. I don't think you can dynamically set those items in LINQ. But i'd like to be proved wrong! – 5uperdan Oct 14 '14 at 11:12

1 Answers1

0
`System.Linq.Dynamic` library will help you generating dynamic query which has extension method

Where(string predicate,params object[] values)

To use this first include the namespace

using System.Linq.Dynamic;

then construct the predicate at run time

string predicate = "OrderId = @0";
int identity_value = 5;

then query db.Orders

db.Orders.AsQueryable().Where(predicate,identity_value);
user3603255
  • 260
  • 1
  • 3
  • 12