将长 if 语句更改为 LINQ

发布于 2024-10-31 21:13:48 字数 413 浏览 0 评论 0原文

我用 C# 编写了这个检查来执行一些值检查:

           if ((x <= 20 && y <= 5) || (x <= 30 && y <= 10) ||
           (x <= 50 && y <= 15) || (x <= 70 && y <= 20) ||
           (x <= 70 && y <= 30))
             {
                // do something
              }

我正在学习 LINQ,我想更改上面的代码以使用 LINQ,

如果您提交代码,请您添加一些注释来解释一下转换。

I wrote this check in C# to perform some value checking:

           if ((x <= 20 && y <= 5) || (x <= 30 && y <= 10) ||
           (x <= 50 && y <= 15) || (x <= 70 && y <= 20) ||
           (x <= 70 && y <= 30))
             {
                // do something
              }

I'm in the process to learn LINQ, and I would like to change the above code to use LINQ,

If you submit a code, can you please add some comment to explain the conversion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏已如深秋| 2024-11-07 21:13:48

如果您确实想使用 LINQ,一种方法是存储代表每种情况(上限)的元组集合,然后使用 Enumerable.Any 方法来测试 (x, y) 对满足任何可能的条件。

示例(实际上,您可以创建一次集合并赋予它更长的生命周期,也许可以通过字段引用):

var bounds = new Dictionary<int, int>
{
    {20, 5},
    {30, 10},
    {50, 15},
    {70, 20},
    {70, 30}
}.Select(kvp => new { XUpperBound = kvp.Key, YUpperBound = kvp.Value });


if (bounds.Any(tuple => x <= tuple.XUpperBound && y <= tuple.YUpperBound))
{
    ...
}

If you really wanted to use LINQ, one way would be to store a collection of tuples that represented each of the cases (upper-bounds) and then use the Enumerable.Any method to test if an (x, y) pair met any of the possible conditions.

Example (in reality, you can create the collection once and give it longer lifetime, perhaps through a field-reference):

var bounds = new Dictionary<int, int>
{
    {20, 5},
    {30, 10},
    {50, 15},
    {70, 20},
    {70, 30}
}.Select(kvp => new { XUpperBound = kvp.Key, YUpperBound = kvp.Value });


if (bounds.Any(tuple => x <= tuple.XUpperBound && y <= tuple.YUpperBound))
{
    ...
}
凯凯我们等你回来 2024-11-07 21:13:48

LINQ 与您发布的代码没有任何关系。由于您选择的数字似乎在很大程度上是任意的,因此我看不出有什么方法可以进一步减少该陈述。

为了使它看起来更好,只需格式化代码:

if  (
    (x <= 20 && y <= 5) || 
    (x <= 30 && y <= 10) ||
    (x <= 50 && y <= 15) || 
    (x <= 70 && y <= 20) ||
    (x <= 70 && y <= 30)
    )
        {
            // do something
        }

这应该使您的对清晰,使添加条件变得容易,同时不会仅仅为了使用 LINQ 而增加 LINQ 的开销。

LINQ doesn't have anything to do with the code that you've posted. Since the numbers you've chosen seem to be largely arbitrary, I don't see a way of reducing that statement any further.

To make it look better, just format the code:

if  (
    (x <= 20 && y <= 5) || 
    (x <= 30 && y <= 10) ||
    (x <= 50 && y <= 15) || 
    (x <= 70 && y <= 20) ||
    (x <= 70 && y <= 30)
    )
        {
            // do something
        }

This should make your pairs clear, make it easy to add conditions, while not adding the overhead of LINQ just for the sake of using it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文