如何使用LINQ或lambdas实现?

时间:2017-03-23 20:52:45

标签: c# linq lambda boolean expression

我很好奇是否可以使用LINQ语句或lambda实现以下内容。我是两个新手,所以我不太确定如何编码。目的是使Func<>委托我可以添加多个回调,并将它们全部计算为所有返回值的布尔AND表达式。优选的解决方案将允许单个假短路来评估其余的评估。我以暴力方式实现了这一点,代码和输出如下所示:

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace TestHarness
{
    public partial class Form1 : Form
    {
        Func<bool, bool, bool, bool> validPosition;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            validPosition += Func1;
            validPosition += Func2;
            validPosition += Func3;

            Delegate[] delegates = validPosition.GetInvocationList();

            bool composite = true;
            int i = 0;

            foreach (Func<bool, bool, bool, bool> d in delegates)
            {
                composite = composite & d(false, true, true);
                if (!composite)
                    break;
                ++i;
            }
            Debug.WriteLine("Result: " + composite + "\tIterations: " + i);

            composite = true;
            i = 0;
            foreach (Func<bool, bool, bool, bool> d in delegates)
            {
                composite = composite & d(true, false, true);
                if (!composite)
                    break;
                ++i;
            }
            Debug.WriteLine("Result: " + composite + "\tIterations: " + i);

            composite = true;
            i = 0;
            foreach (Func<bool, bool, bool, bool> d in delegates)
            {
                composite = composite & d(true, true, false);
                if (!composite)
                    break;
                ++i;
            }
            Debug.WriteLine("Result: " + composite + "\tIterations: " + i);

            composite = true;
            i = 0;
            foreach (Func<bool, bool, bool, bool> d in delegates)
            {
                composite = composite & d(true, true, true);
                if (!composite)
                    break;
                ++i;
            }
            Debug.WriteLine("Result: " + composite + "\tIterations: " + i);
        }

        bool Func1(bool a, bool b, bool c)
        {
            return a;
        }
        bool Func2(bool a, bool b, bool c)
        {
            return b;
        }
        bool Func3(bool a, bool b, bool c)
        {
            return c;
        }
    }
}

Output:

Result: False   Iterations: 0
Result: False   Iterations: 1
Result: False   Iterations: 2
Result: True    Iterations: 3

感谢您的任何建议。

1 个答案:

答案 0 :(得分:2)

我个人会创建一个自定义扩展方法并将逻辑放在里面(最有可能在样本中使用 var model = db.Persons .Include(x => x.PersonsRoles) .FirstOrDefault(x => x.PersonId == person.PersonId ); db.TryUpdateManyToMany(model.PersonsRoles, listOfNewRoleIds .Select(x => new PersonRole { RoleId = x, PersonId = person.PersonId }), x => x.PersonId); ,以避免闭包和lambda开销)。但由于您对LINQ解决方案感兴趣,All扩展方法正是您所需要的:

结果:

  

true 如果源序列的每个元素都通过指定谓词中的测试,或者序列为空;否则, false

短路:

  

一旦确定结果,就会停止源的枚举。

等效示例LINQ代码:

foreach