'How to do Entity Framework/Linq join in query with where clause expressions
I have a Entity Framework query where the where clauses' change based on several selections.
Here is my code:
Expression<Func<CtCsGroup, bool>> where_expression1 = x => 1 == 1;
Expression<Func<CtCsGroup, bool>> where_expression2 = x => 1 == 1;
Expression<Func<CtCsGroup, bool>> where_expression3 = x => 1 == 1;
if (selections.SearchCSGroup.NoNull() != "")
{
where_expression1 = x => x.CsGroup.ToUpper() == selections.SearchCSGroup.ToUpper();
}
if (selections.SearchUserId.NoNull() != "")
{
where_expression3 = x => x.UsrIdn.ToUpper() == selections.SearchUserId.ToUpper();
}
if (!selections.SearchIncludeRetires)
{
where_expression2 = x => x.CsRetire != "Y";
}
var groupDTOs = (from g in _infobaseContext.CtCsGroup
.Where(where_expression1)
.Where(where_expression2)
.Where(where_expression3)
select new
{
UsrIdn = g.UsrIdn,
CsGroup = g.CsGroup,
CsRetire = g.CsRetire,
CsGroupName = "test" // n.CsGroupName
}).ToList();
I want to add a join to another table.
How do I add this to the above query? I have tried several different formats but whenever I add it, I get an error "(where_expression1)" ,"(where_expression3)", etc.
Thanks.
Here are some more details...
var groupDTOs = (from g in _infobaseContext.CtCsGroup
join n in _infobaseContext.TblCsGroupList
on g.CsGroup equals n.CsGroup
select new
{
UsrIdn = g.UsrIdn,
CsGroup = g.CsGroup,
CsRetire = g.CsRetire,
CsGroupName = n.CsGroupName
}).ToList();
- and this one works without the join -
var groupDTOs = (from g in _infobaseContext.CtCsGroup
.Where(where_expression1)
select new
{
UsrIdn = g.UsrIdn,
CsGroup = g.CsGroup,
CsRetire = g.CsRetire,
CsGroupName = "test" //n.CsGroupName
}).ToList();
- but when I add both the join and the expression -
var groupDTOs = (from g in _infobaseContext.CtCsGroup
join n in _infobaseContext.TblCsGroupList
on g.CsGroup equals n.CsGroup
.Where(where_expression1)
select new
{
UsrIdn = g.UsrIdn,
CsGroup = g.CsGroup,
CsRetire = g.CsRetire,
CsGroupName = n.CsGroupName
}).ToList();
- Visual Studio is giving this error:
Argument 2: cannont convert from 'System.Linq.Expressions<System.Func<IBDataServices.Models.CtCsGroup, bool>>' to 'System.Func<char,bool>'

Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
