本文介绍了如果否则条件在linq查询中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有这个代码: var vr =( 来自 context.GRI_Package // where(ISSTUDENT == true?s.StudentID == StudentUserID:s.TutorUserID == TutorUserID) // where(ISSTUDENT == true?s .StudentID == StudentUserID:(s.TutorUserID == TutorUserID&& s.StudentID == StudentUserID )) 其中(ISSTUDENT == true ?(s.TutorUserID = = 0 || s.StudentID == StudentUserID):( s.TutorUserID == TutorUserID&& s.StudentID == StudentUserID)) // 其中s.TutorUserID == TutorUserID&& s.StudentID == StudentUserID 选择 new GetSessionpackageInfo { PackageID = s.PackageID, })。ToList(); vr。转储(); 这里如果其他条件不是wokring有没有语法错误? 请帮帮我 我尝试了什么: 我试过这个条件 where(ISSTUDENT == true?(s.TutorUserID == 0 || s.StudentID == StudentUserID):( s.TutorUserID == TutorUserID&& s.StudentID == StudentUserID))解决方案 问题是您正在尝试在where子句中使用过程控制流逻辑。您需要了解WEHRE子句中的内容是布尔表达式而不是语句。 我将解释您之前的一个&更简单的尝试,你已经注释掉了。你有两个选择。对你来说更自然的可能就是在这样的C#代码中做一个IF: var query = context.GRI_Package; if (ISSTUDENT) { query = query.Where(s = > ; s.StudentID == StudentUserID); } else { query = query.Where(s = > s.TutorUserID == TutorUserID); } var vr = query.Select(s = > new GetSessionpackageInfo {PackageID = s.PackageID})。ToList(); 或者你可以拥有全部where子句但它必须是布尔表达而不是声明: var vr =( 来自 s context.GRI_Package 其中(ISSTUDENT == true && s.StudentID == StudentUserID) ||(ISSTUDENT == false && s.TutorUserID == TutorUserID) 选择 new GetSessionpackageInfo { PackageID = s.PackageID,})。ToList(); I have this code :var vr = (from s in context.GRI_Package //where (ISSTUDENT == true ? s.StudentID == StudentUserID : s.TutorUserID == TutorUserID) //where (ISSTUDENT == true ? s.StudentID == StudentUserID : (s.TutorUserID == TutorUserID && s.StudentID == StudentUserID)) where (ISSTUDENT == true ? (s.TutorUserID == 0 || s.StudentID == StudentUserID) : (s.TutorUserID == TutorUserID && s.StudentID == StudentUserID)) //where s.TutorUserID == TutorUserID && s.StudentID == StudentUserID select new GetSessionpackageInfo { PackageID = s.PackageID,}).ToList();vr.Dump();here if else condition is not wokring is there any syntax mistake ?please help meWhat I have tried:I tried this conditionwhere (ISSTUDENT == true ? (s.TutorUserID == 0 || s.StudentID == StudentUserID) : (s.TutorUserID == TutorUserID && s.StudentID == StudentUserID)) 解决方案 The problem is that you are trying to use procedural control flow logic in the where clause. You need to understand that what's in the WEHRE clause is a boolean expression not a statement.I will explain on one of your earlier & simpler attempts, which you have commented out. You have two options. More natural for you would be maybe do an IF in the C# code like this:var query = context.GRI_Package;if (ISSTUDENT){ query = query.Where(s => s.StudentID == StudentUserID);}else{ query = query.Where(s => s.TutorUserID == TutorUserID);}var vr = query.Select(s => new GetSessionpackageInfo {PackageID = s.PackageID}).ToList();Or you can have is all in the where clause but then it needs to be a boolean expresson and not a statement:var vr = (from s in context.GRI_Package where (ISSTUDENT == true && s.StudentID == StudentUserID) || (ISSTUDENT == false && s.TutorUserID == TutorUserID) select new GetSessionpackageInfo { PackageID = s.PackageID, }).ToList(); 这篇关于如果否则条件在linq查询中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-04 19:08