0

I am taking the input from user and store in variable. then I am selecting the data from database using LINQ to do that I have to perform a join condition and the property of that join should change according to what user enter if user select location then it should be e.Location and if he select department it should be e.department . the join condition both the side is a INT and while storing the data in variable I am getting the value in string, for example user select location so the variable is string variable , so on join it should be changed to INT to perform proper join. In my model class I have 4 property location, department, designation, Etiolation. from front end user select any of this so it should do store in variable then it fetch value from database I have written the below query. it should join with other table to fetch value so this is a case, if user select any of this it should be join with other table for that I want to use variable to join the table .

before making the value change according to user select .

var param = form["Category"];
var query =  (from e in table1 join c in table2 on e.location equals c.Id select e);

doing some thing with the query data

and how I want it to be ,

var param = form["Category"];
var query =  (from e in table1 join c in table2 on e.param equals c.Id select e);
Admin
  • 1
  • 3
  • in the second query e.param is used and i am not able to use the variable name in the place of property of e – Admin Jan 10 '23 at 08:03
  • **param** is variable which store the value location which is in string and writing it in above form as you suggested it say wrong property – Admin Jan 10 '23 at 08:25
  • **EF.Property called with wrong property name** is the reasone due to what my query is not giving any result if know the solution let me know. – Admin Jan 10 '23 at 08:34
  • 2
    I'm sorry but I'm finding it really difficult to understand what you mean. Could you try rewriting the question or perhaps using a bit more punctuation? – sr28 Jan 10 '23 at 08:52
  • sorry for wrong formatting , I have changed question explain in details go through it. – Admin Jan 10 '23 at 09:08
  • Try looking at this: https://stackoverflow.com/questions/14200778/dynamic-query-using-linq-to-sql – sr28 Jan 10 '23 at 09:17
  • I tried that but not get what I wanted using this https://stackoverflow.com/questions/14200778/dynamic-query-using-linq-to-sql . and I have provided the model filed in question. – Admin Jan 10 '23 at 09:27
  • So you tried creating a dynamic string query and then executing it? – sr28 Jan 10 '23 at 09:41
  • yes @sr28 I am trying to create a dynamic string let me know solution . – Admin Jan 10 '23 at 09:59
  • If it's just to create a dynamic string then you can do var query = $"(from e in table1 join c in table2 on {param} equals c.Id select e)"; – sr28 Jan 10 '23 at 10:01
  • but I have a very big query which uses multiple from and union() and many more function which are not supported by string queries is there no way to change the property name with the variable which store its value – Admin Jan 10 '23 at 10:16
  • "many more function which are not supported by string queries" can you give an example? – sr28 Jan 10 '23 at 14:04
  • I am using multiple from, let and join clauses and it's using the Enumerable.Range() function, .AddMonths(), .Union(), .GroupBy() functions which are not supported in dynamic string query. @sr28 – Admin Jan 11 '23 at 04:10

0 Answers0