开发者

Nullable Parameter used in Lambda Query is being ignored

I'm trying to pass a null value from a RenderAction to another view. But in between, at the controller, my linq lambda expression is not loading the right field, despite the null value going through correctly..

SprintManager.cshtml

<div id="Global_Backlog_Board" class="Board_Panel">
    @{Html.RenderAction("ListOfSingleCards", new 
        { 
            State_ID = 1
        });}
</div>

HomeController.cs

public PartialViewResult ListOfSingleCards( int? Sprint_ID,
                                            int State_ID = 1)
{
    var Cards = db.Cards.Where(x => x.State_ID == State_ID &&
                                    x.Sprint_ID == Sprint_ID && 
                                    x.Deleted != 1 && 
                                    x.Archive != 1).ToList();
    return PartialView(Cards);
}

So Sprint_ID is being passed over and loaded as null here, but I can't get the query to load the rows correctly.

In fact, the following works:

var Cards = db.Cards.Where(x => x.State_ID == State_ID &&
            x.Sprint_ID == null && 
            x.Deleted != 1 && 
            x.Archive != 1).ToList();

So I suppose I could check if Sprint_ID is null and depending on the result run one of the two seperate queries, 开发者_JS百科but I'd like to understand why my original attempt is not working.

Thank you!


I don't know the correct answer but based on your solution you should be able to tidy it up:

var cards = new List<Card>();
var query = db.Cards.Where(x => x.State_ID == State_ID &&
                                x.Deleted != 1 &&
                                x.Archive != 1);

if (Sprint_ID.HasValue) 
    query = query.Where(x => x.Sprint_ID == Sprint_ID);
else
    query = query.Where(x => x.Sprint_ID == null);

cards = query.ToList();


A nullable int won't return "null" in the way that you're thinking. You have to check the HasValue property of it to determine if there is a value, and if so then use it otherwise use null:

public PartialViewResult ListOfSingleCards( int? Sprint_ID,
                                            int State_ID = 1)
{
    var Cards = db.Cards.Where(x => x.State_ID == State_ID &&
                                    x.Sprint_ID == Sprint_ID.HasValue ? Sprint_ID.Value : null && 
                                    x.Deleted != 1 && 
                                    x.Archive != 1).ToList();
    return PartialView(Cards);
}


Until something better comes a long, I'm using this:

var Cards = new List<Card>();

if (Sprint_ID == null)
{
    Cards = db.Cards.Where(x => x.State_ID == State_ID &&
                                x.Sprint_ID == null &&
                                x.Deleted != 1 &&
                                x.Archive != 1).ToList();
}
else
{
    Cards = db.Cards.Where(x => x.State_ID == State_ID &&
                                x.Sprint_ID == Sprint_ID &&
                                x.Deleted != 1 &&
                                x.Archive != 1).ToList();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜