Possible Duplicate:
build linq queries dynamically
linq update query dynamically
I have a method passing code id, e.g. if code – 1 then i select data from tbl A and if code – 2 I want to select from tbl B
e.g. I tried to do: but the code doesnt work
public static void Update(int code, int userid)
{
var query =
(from t1 in code == 1? dataContext.tb1 : dataContext.tb2
where t1.Id == userid
select t1).SingleOrDefault();
}
how can I generate query dynamically?
Thanks
It is not obvious from your question but is sounds like you are using some linq wrapper on database access. If this is the case then it is most likely that you cannot crate query that has condition on source of data. The easiest way to crate such if is to crate two queries as simple as that:
Edit
On now it obvious that you are using linq to sql so the above stands. But your error is easier then that you have ternary operator that returns two different types on true and false side and that is the error.