C# linq lambda 表达式和“&”运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14270129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:16:12  来源:igfitidea点击:

linq lambda expression and '&' operator

c#linq

提问by Gregg

I'm working on a ASP.NET MVC (an older version) application and I have a form where students are assigned an adviser based on a major code AND by alpha of a student's last name.

我正在开发一个 ASP.NET MVC(旧版本)应用程序,我有一个表格,其中根据主要代码和学生姓氏的字母为学生分配了一名顾问。

I am trying to implement the below code but it is showing red scribbles to &operator:

我正在尝试实现以下代码,但它向&操作员显示红色涂鸦:

Error: operator '&' cannot be applied to operands of type 'System.Linq.IQueryable<Appointments.Models.MajorAdviserStudentAssignmentByAlpha>' and 'lambda expression' and 'lambda expression'

错误:运算符“&”不能应用于“ System.Linq.IQueryable<Appointments.Models.MajorAdviserStudentAssignmentByAlpha>”和“lambda 表达式”和“lambda 表达式”类型的操作数

var majorAssignmentByAlpha = FindAllMajorAdviserStudentAssignmentByAlphas()
    .Where(
           a => a.MajorString == student.StudentMajor) &
           (a => ((String.Compare(student.StudentLastName, a.AlphaStart) >= 0) &
                 (String.Compare(student.StudentLastName, a.AlphaEnd) <= 0))
    .FirstOrDefault());

if (majorAssignmentByAlpha != null)
    return majorAssignmentByAlpha.Adviser;

I would appreciate any help to understand what's wrong here. Thanks in advance!

我将不胜感激任何帮助了解这里出了什么问题。提前致谢!

采纳答案by rexcfnghk

I would like to add a correction here: actually both &&and&areboolean AND operators in C# (when used with bool). However the former will do a short-circuit evaluation:

我想在这里补充修正:其实两者&&&在C#(当用于布尔AND运算符bool)。但是前者会做短路评估

Consider A && B, if Ais falsethen Bwill not be evaluated since the result will be falseno matter what is the truthness of B.

考虑一下A && B,如果Ais falsethenB将不会被评估,因为false无论 的真实性如何,结果都将是B

And for the error you are getting, probably that's because your &operator is comparing a boolean expression and a lambda expression (function). Try this

对于您遇到的错误,可能是因为您的&运算符正在比较布尔表达式和 lambda 表达式(函数)。尝试这个

MajorAdviserStudentAssignmentByAlpha majorAssignmentByAlpha = FindAllMajorAdviserStudentAssignmentByAlphas().FirstOrDefault(
        a => a.MajorString == student.StudentMajor & String.Compare(student.StudentLastName, a.AlphaStart) >= 0 &
             String.Compare(student.StudentLastName, a.AlphaEnd) <= 0);

    if (majorAssignmentByAlpha != null)
        return majorAssignmentByAlpha.Adviser;

回答by Bryan Crosby

I think you meant to use &&for the logical AND operation.

我认为您打算&&用于逻辑 AND 运算。

FirstOrDefault()also takes a Func<T, bool>so you could do:

FirstOrDefault()也需要一个Func<T, bool>所以你可以这样做:

var majorAssignmentByAlpha = FindAllMajorAdviserStudentAssignmentByAlphas().FirstOrDefault(
            a => a.MajorString == student.StudentMajor &&
            (a => ((String.Compare(student.StudentLastName, a.AlphaStart) >= 0) &&
                 (String.Compare(student.StudentLastName, a.AlphaEnd) <= 0));

回答by Jorge

That's because the operator that you're looking it's &&. Refactor your Linq expresssion like this

那是因为您正在查找的运算符是&&. 像这样重构你的 Linq 表达式

MajorAdviserStudentAssignmentByAlpha majorAssignmentByAlpha = FindAllMajorAdviserStudentAssignmentByAlphas().Where(
        a => a.MajorString == student.StudentMajor) &&
        (a => ((String.Compare(student.StudentLastName, a.AlphaStart) >= 0) &&
             (String.Compare(student.StudentLastName, a.AlphaEnd) <= 0)).FirstOrDefault());

    if (majorAssignmentByAlpha != null)
        return majorAssignmentByAlpha.Adviser;

回答by TheEvilPenguin

I think you're added too many parentheses. Try this:

我认为您添加了太多括号。尝试这个:

MajorAdviserStudentAssignmentByAlpha majorAssignmentByAlpha = FindAllMajorAdviserStudentAssignmentByAlphas().Where(
            a => a.MajorString == student.StudentMajor &&
                 String.Compare(student.StudentLastName, a.AlphaStart >= 0) &
                 String.Compare(student.StudentLastName, a.AlphaEnd <= 0)).FirstOrDefault();

if (majorAssignmentByAlpha != null)
    return majorAssignmentByAlpha.Adviser;

.Where() takes a single expression returning bool. You are trying to and together two different expressions. If you remove the close parentheses before the &&, the open parentheses after and the second a =>, you're anding two boolean statements, which works.

.Where() 接受单个表达式返回bool. 您正在尝试将两种不同的表达方式结合在一起。如果您删除 之前的右&&括号、之后的a =>左括号和第二个,则您将使用两个布尔语句,这是有效的。