Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello experts
I have a query and everything in my query works well without the last condition, I use the last condition to filter the joined tables, but I get this error: Operator == cannot be applied to operands of type IEnumerable<string> and string.
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 }).Distinct() 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;


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

What I have tried:

When I use the condition in the second join, I get a wrong result, and the condition must be applied in the last join, which unfortunately leads to an error.
Posted
Updated 11-Feb-24 11:15am
v3
Comments
Jo_vb.net 11-Feb-24 9:51am    
Perhaps you need to convert it first to string?

where s.Result.ToString() == "Accepted" && s.TypeofRest.ToString() == "Primary"
OriginalGriff 11-Feb-24 10:55am    
I think you probably want to start by showing sample input and expected output: we don't have access to your DB, so we can't tell what you are actually trying to do.

Make samples as short and simple as possible (while still showing the problem) to make it easier for reader to work out what you are trying to do. And try to explain where the results you want come from!

Just saying "I get the error but I think I want it on the last join anyway, not the second" doesn;t help us much.

Use the "Improve question" widget to edit your question and provide better information.
Max Speed 2022 11-Feb-24 17:16pm    
i did pleas check that again <3 thank you for helping

It seems to me that your problem here is in this line of code:


C#"
TypeofRest = g.Select(x => x.TypeofRest), Result = g.Select(x => x.Result) })

The Select method returns an IEnumerable so both TypeofRest and Result are both IEnumerables. The error arises because you are comparing them with a single string instance in this line of code:


C#"
where s.Result == "Accepted" && s.TypeofRest == "Primary"


I would suggest that you filter the enumerables so that they return a single string.

 
Share this answer
 
v3
 
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