Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have something like this where I need the selected filter variables to run at the runtime inside the LINQ statement, and I know the LING is not doing as I anticipate. Any answer would be appreciated.

C#
//code starts here.......
string strOnGameWhere = " && t.GameType.Contains(Texas)";

var tournament = from t in tournamentName
                             where t.BeachBar == "Bar" + strOnGameWhere
                             select t;
Posted
Updated 17-Aug-11 0:31am
v3
Comments
Simon_Whale 17-Aug-11 6:51am    
In what way isn't it doing what you want? does it error? does it return incorrect results?

You can do this and many more by using expression trees in Linq. Click Here[^] for detail or Just google Google it[^].
 
Share this answer
 
I had the same problem earlier. Solution 1 is correct.
This forum post helped me a lot to achieve my goal:
http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/5edb3dd0-c778-47e2-b89d-a9c90a0bd1bc/
 
Share this answer
 
One obvious thing that 'leaps out' at me looking at your code is that 'strOnGameWhere' is a string: when you add it to the string 'Bar,' of course you just get another string.

Clearly what you intend to do is to have the LINQ evaluation in progress evaluate the condition
&& t.GameType.Contains(Texas)
as part of the 'Where' clause, so what is executed is:
where t.BeachBar == "Bar" && t.GameType.Contains(Texas)
So, to me, the issue you are dealing with is how to call a variable function in a LINQ query that is executed inside a Where clause.

In LINQ terms, as I understand them, that means the LINQ expression must 'capture' an 'outer' variable reference, in this case ... a function ? ... a lambda ?

For understanding that, and so much else in .NET, I suggest you get and read Jon Skeet's masterpiece, "C# in Depth" (Manning Press, 2nd. edition).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900