Here is an example query that I am trying to convert to LINQ:
SELECT * FROM Users WHERE Users.lastname LIKE '%fra%' AND Users.Id IN ( SELECT UserId FROM CompanyRolesToUsers WHERE CompanyRoleId in (2,3,4) )
There is an FK relationship between CompanyRolesToUsers and Users , but this relationship is many, many, and CompanyRolesToUsers is a join table.
We already have most of our site, and we already have most of the filtering, working by creating expressions using the PredicateExtensions class.
The code for simple filters looks something like this:
if (!string.IsNullOrEmpty(TextBoxLastName.Text)) { predicateAnd = predicateAnd.And(c => c.LastName.Contains( TextBoxLastName.Text.Trim())); } e.Result = context.Users.Where(predicateAnd);
I am trying to add a predicate for a subquery in another table. ( CompanyRolesToUsers )
What I would like to add is what this does:
int[] selectedRoles = GetSelectedRoles(); if( selectedRoles.Length > 0 ) { //somehow only select the userid from here ???: var subquery = from u in CompanyRolesToUsers where u.RoleID in selectedRoles select u.UserId; //somehow transform this into an Expression ???: var subExpression = Expression.Invoke(subquery); //and add it on to the existing expressions ???: predicateAnd = predicateAnd.And(subExpression); }
Is there any way to do this? This is frustrating because I can easily write a stored procedure, but I am new to this LINQ function and I have a deadline. I could not find an example that matches, but I am sure that it is somewhere there.
c # linq linq-to-sql
marcel_g Jan 06 '09 at 23:27 2009-01-06 23:27
source share