简体   繁体   English

即使我正在过滤掉一个条件,Linq Where 子句也会返回所有内容

[英]Linq Where clause returns everything even though I am filtering out a condition

I am using c# and SolrNet to return a list of games that gamers are playing.我正在使用 c# 和 SolrNet 返回游戏玩家正在玩的游戏列表。

In my c# code, I am trying to return data for PlayerHistory , where IsPlaying is true.在我的 c# 代码中,我试图返回PlayerHistory的数据,其中IsPlaying为真。

The database has a table for Players with a bit column (0 for false, 1 for true) for IsPlaying .数据库有一个 Players 表,其中包含IsPlaying的位列(0 表示 false,1 表示 true)。

Here is my code:这是我的代码:

var nodes = GetGamerNodes().Select(MapNode);
    Solr.AddRange(nodes);
    Solr.Commit();

private static SearchNodes MapNode(GameList node)
{
    return new SearchNodes {
        Id = node.Id.ToString(),
        GameTitle = node.Title,
        Story = node.Story,
        Published = node.Game.Published,
        PlayerHistory= node.Players.Where(e => e.Player.Current.IsPlaying).Select(e => e.Player.Name).ToArray()
    };
}

PlayerHistory is an ICollection PlayerHistory是一个ICollection

In my model, IsPlaying is defined like this:在我的模型中, IsPlaying是这样定义的:

public virtual bool IsPlaying { get; set; }

But it is returning PlayerHistory data for everything regardless of the value of IsPlaying .但无论IsPlaying的值如何,它都会返回所有内容的PlayerHistory数据。

Am I doing something wrong?难道我做错了什么?

Thanks!谢谢!

Here's a little trick that has helped me at times like these.这里有一个小技巧,在这样的时候帮助了我。

What works for me is to make a temporary predicate like this:对我有用的是做一个这样的临时谓词:

bool debugGetIsPlayerPlaying(Player player)
{
    return player.Current.IsPlaying;
}

and then when you do this replacement...然后当你做这个替换时......

private static SearchNodes MapNode(GameList node)
{
    return new SearchNodes
    {
        Id = node.Id.ToString(),
        GameTitle = node.Title,
        Story = node.Story,
        Published = node.Game.Published,
        PlayerHistory = node.Players.Where(e => debugGetIsPlayerPlaying(e.Player)).Select(e => e.Player.Name).ToArray()
    };            
}

... it gives you something you can set breakpoints on and step through to help you determine exactly what's going on in the failing Linq query. ...它为您提供了一些可以设置断点并逐步执行的内容,以帮助您准确确定失败的 Linq 查询中发生了什么。 Hope this is helpful!希望这有帮助!

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

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