简体   繁体   English

Linq Any返回true,尽管集合中的所有值均为false

[英]Linq Any returns true despite all values in the collection being false

I see very strange behavior in properties value calculation. 我在属性值计算中看到非常奇怪的行为。 I have a property HasChanged, which is true if any of it's dependent properties is true. 我有一个属性HasChanged,如果它的任何依赖属性为true,则为true。 But I got a result - all arguments are false and the result is true. 但是我得到了一个结果-所有参数都为假,结果为真。 I am using MVVM Light framework, and each property is INotifyPropertyChanged 我正在使用MVVM Light框架,并且每个属性都是INotifyPropertyChanged

Here are helper functions 这是助手功能

    private static bool PropertyWrapper(bool value, [CallerMemberName] string callerName = "")
    {
        Logger.Debug($"[{callerName}: {value}]");
        return value;
    }

    private static T PropertyWrapper<T>(Expression<Func<T>> property)
    {
        var compiled = property.Compile();
        var result = (T)compiled.DynamicInvoke();
        Logger.Debug($"[{GetName(property)}: {result}]");
        return result;
    }

    private static string GetName<T>(Expression<Func<T>> expr)
    {
        var mexpr = expr.Body as MemberExpression;
        if (mexpr == null) return "(null)";
        if (mexpr.Member == null) return "((null))";
        return mexpr.Member.Name;
    }

And this is the code 这是代码

    public virtual bool HasChanged => PropertyWrapper(new[] {
        PropertyWrapper(() => TitleChanged),
        PropertyWrapper(() => EnglishTitleChanged),
        PropertyWrapper(() => OriginalTitleChanged),
        PropertyWrapper(() => PlotChanged),
        PropertyWrapper(() => OutlineChanged),
        PropertyWrapper(() => DirectorChanged),
        PropertyWrapper(() => YearChanged),
        PropertyWrapper(() => MovieRolesChanged),
        PropertyWrapper(() => GenresChanged),
        PropertyWrapper(() => CountriesChanged)
    }.Any(), "HasChanged");

    public bool YearChanged => this.state == State.Edit && this.source.MovieDescription.Year != this.clone.MovieDescription.Year;
    public bool TitleChanged => HasTitleChanges();
    public bool EnglishTitleChanged => HasEnglishTitleChanges();
    public bool OriginalTitleChanged => HasOriginalTitleChanges();
    public bool PlotChanged => HasDescriptionChanges();
    public bool DirectorChanged => HasDirectorChanges();
    public bool OutlineChanged => HasOutlineChanges();
    public bool MovieRolesChanged => HasMovieRolesChanges();
    public bool CountriesChanged => HasCountriesChanges();
    public bool GenresChanged => HasGenresChanges();

and what is written to log 以及写入日志的内容

[TitleChanged: False] [TitleChanged:False]

[EnglishTitleChanged: False] [EnglishTitleChanged:False]

[OriginalTitleChanged: False] [OriginalTitleChanged:False]

[PlotChanged: False] [PlotChanged:错误]

[OutlineChanged: False] [OutlineChanged:False]

[DirectorChanged: False] [DirectorChanged:False]

[YearChanged: False] [YearChanged:False]

[MovieRolesChanged: False] [MovieRolesChanged:False]

[GenresChanged: False] [GenresChanged:False]

[CountriesChanged: False] [CountriesChanged:False]

[HasChanged: True] [已更改:正确]

It looks impossible dull, but I can't imagine how can it be. 看起来不可能是无聊的,但我无法想象怎么可能。 Please, explain me the reason of such behavior. 请给我解释这种行为的原因。

Any without parameters returns if there are any elements in the collection. 如果集合中有任何元素,则返回Any不带参数的元素。 To get what you want you have to check the value of your elements as your Any predicate: 要获得所需的内容,您必须检查元素的值作为Any谓词:

public virtual bool HasChanged => PropertyWrapper(new[] {
       //...
       }.Any(q => q), "HasChanged");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM