I have a query and everything in my query works well without the last condition
(where s.Result == "Accepted" && s.TypeofRest == "Primary"
)
, I use the last condition to filter the joined tables, but I get this error: Operator == cannot be applied to operands of type IEnumerable and string. that's my DB tables: Rest_TBl: ID(P.K), UserNO(F.K), Days, TypeOfRest, Result
User_Tbl: UserName, UserNO(P.K)
explain: The two Rest_Tbl and User_Tbl tables are related by the UserNO field, and I want to join the Rest_Tbl table with the User_Tbl table once to access the UserName field and join the User_Tbl table with itself once to calculate the days field, I want to sum(days)field based on distinct UserNo. this is my code:
var query = from t in db.User_Tbl
join f in db.Rest_Tbl.Select(x => new { x.UserNO, x.Result, x.TypeofRest }) on t.UserNO equals f.UserNO
join s in db.Rest_Tbl.Select(t => new { t.UserNO, t.ID, t.Days, t.Edate, t.Bdate, t.Category, t.Result, t.TypeofRest }).GroupBy(x => x.UserNO)
.Select(g => new { UserNO = g.Key, Days = g.Sum(x => x.Days), ID = g.Key, TypeofRest = g.Select(x => x.TypeofRest), Result = g.Select(x => x.Result) })
on f.UserNO equals s.UserNO
where s.Result == "Accepted" && s.TypeofRest == "Primary"
select new
{
ID = s.ID,
UserNO = t.UserNO,
UserName = t.UserName,
Days = s.Days
};
var result = query.Distinct().ToList();
MyDataGrid.ItemsSource = result;
The TypeofRest and Result in your anonymous type are collections not single values. you need to change your condition to check if the collection contains the desired value. You can use the .Contains() (.Contain Function in MVC) method for this purpose.Like this:
var query = from t in db.User_Tbl
join f in db.Rest_Tbl
on t.UserNO equals f.UserNO
join s in db.Rest_Tbl
.GroupBy(x => x.UserNO)
.Select(g => new
{
UserNO = g.Key,
Days = g.Sum(x => x.Days),
TypeofRest = g.Select(x => x.TypeofRest),
Result = g.Select(x => x.Result)
})
on f.UserNO equals s.UserNO
where s.Result.Contains("Accepted") && s.TypeofRest.Contains("Primary")
select new
{
ID = f.ID,
UserNO = t.UserNO,
UserName = t.UserName,
Days = s.Days
};
var result = query.Distinct().ToList();
MyDataGrid.ItemsSource = result;