具有不返回正确结果的条件的List.Count [英] List.Count with condition not returning a correct result

查看:91
本文介绍了具有不返回正确结果的条件的List.Count的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我目前正在从事一项课程任务,而且我遇到了一些我无法解决的事情。



我正在使用一个列表来跟踪所做的请求,每个请求都存在一个目标(int)以及一个方向(枚举)。在某些时候我想检查一下这个列表。



但是我没有得到调试时我期待的结果。虽然在我看来下面的代码应该返回0的计数结果。它返回我的计数结果2.



我将省略所有不相关的代码和专注于令我困惑的事情。



Hi guys,

I'm currently working on a course task and I'm running into something I can't wrap my head around.

I'm using a list to keep track of requests made, each request exists of a destination (int) as well as a direction(enum). At some point in time I want to do a check on that list.

However I'm not getting the result I'm expecting while debugging. While in my opinion the below code should return a count result of 0. It returns me a count result of 2.

I'll leave out all non relevant code and focus on what's puzzling me.

int currentReference = 0;
List<Request> RequestList = new List<Request>;

RequestList.Add(new Request(4, Direction.Down));
RequestList.Add(new Request(2, Direction.Down));

int result = RequestList.Count(request => request.reference > currentReference && request.direction == Direction.Up);





如前所述,上面的结果返回值为2的值。

我是否忽略了一些明显的东西,我是否错误地使用了具有条件的List.Count功能?



提前感谢您查看此内容并将我指回正确的轨道!



以下枚举使用。





As mentioned before the above returns me a value of 2 in result.
Am I overlooking something obvious, am I making incorrect use of the List.Count functionality with a condition?

Thanks in advance for taking a look at this and pointing me back on the right track!

Below the enum used.

public enum Direction
    { 
        Up = 0,
        Down = 1,
        Idle = 2
    }





请求的类下面。





Below the class for the request.

public class Request
    {
        public int reference         
        public Direction direction

        public Request(int reference, Direction direction)
        {
            this.reference = reference;
            this.direction = direction;
        }
    }

推荐答案

通过下载提供的解决方案找到问题:Visual Studio以某种方式丢失了跟踪所需的文件在运行应用程序之前重新编译。清理和重建解决方案解决了这个问题。源代码中没有错误(至少不在代码位置;))/ b $ b

一个不相关的建议:应该考虑零值枚举0是枚举的默认值。通常,赋予它未定义,未知,无效,无或类似的含义是有意义的。在你的情况下,使空闲选项成为零值枚举可能是有意义的。这样就可以更容易地发现代码缺陷或者首先避免它们。
Problem found by downloading the provided Solution: Visual Studio somehow lost track which files required rebuiling prior running the application. Cleaning and rebuilding the solution solved the issue. No bug in the source code (at least not at the code location in question ;) )

One unrelated suggestion: A zero-value-enum should be given consideration because 0 is the default value for enums. Often it makes sense to give it a meaning of "Undefined", "Unknown", "Invalid", "None" or similar. In your case it might make sense to make the "Idle"-option the zero-value-enum. That way it's easier to spot code-flaws or avoid them in the first place.


您没有提供类定义。我相信存在问题。

请求类必须接受 Direction 字段并返回相同的类型,以便能够比较查询中的传递值具有类属性(字段)的值。





You did not provide your class definition. I believe there is a problem.
Request class must acccept Direction field and returns the same type, to be able to compare passed values in a query with value of class property (field).


class Request
{
    private int reference =0;
    private Direction dir = Direction.Idle;

    public Request(int _reference, Direction _dir)
    {
        reference = _reference;
        dir = _dir;
    }

    public int Reference
    {
        get{return reference;}
        set{reference = value;}
    }

    public Direction Direction
    {
        get{return dir;}
        set{dir = value;}
    }

}





我使用LinqPad对您的代码进行了相应的更改,并返回0(零)。



I teseted your code with LinqPad with respective changes and it returns 0 (zero).


这篇关于具有不返回正确结果的条件的List.Count的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆