简体   繁体   English

MVC / LINQ - 嵌套在哪些请求不起作用的地方

[英]MVC/LINQ - nested where request who doesn't work

I encounter a problem with a nested Where clauses request in a Razor view (Asp.net MVC with EF 6) : 我在Razor视图中遇到嵌套Where子句请求的问题(带有EF 6的Asp.net MVC):

Here is my request (Model is an IEnumarable from controller, it has correct values) : 这是我的请求(Model是一个来自控制器的IEnumarable,它有正确的值):

 @foreach (var item in Model.Where(p => (p.user.SellGoods.Where(g => g.IsBuy == false)) != null))

My Good model is : 我的好模特是:

    [Key]
    public String Name { get; set; }

    public String UrlImage { get; set; }

    [MinLength(10), MaxLength(500)]
    public String Description { get; set; }

    public float Price { get; set; }

    [DataType(DataType.Date)]
    public DateTime StartDate { get; set; }

    [DataType(DataType.Date)]
    public Nullable<DateTime> EndOfAuction { get; set; }

    public virtual EnumStrategy Strategy { get; set; }

    public int UserID { get; set; }

    public virtual User user { get; set; }

    public Boolean IsBuy { get; set; }

    public int CurrentAuctionWinner { get; set; }

And my User model : 我的用户模型:

    [EmailAddress]
    public String Mail { get; set; }

    [Column("SellGoods")]
    public virtual ICollection<Good> SellGoods { get; set; }

    [Column("BuyGoods")]
    public virtual ICollection<Good> BuyGoods { get; set; }

    public virtual Address ShippingAddress { get; set; }

    public virtual Address BillingAdress { get; set; }

    [Range(0.00d, 10.00d)]
    public double Rate { get; set; }

With my current request, I got all the Good sell by one member like the (g => g.IsBuy == false) doesn't work.. 根据我目前的要求,我得到一个成员的所有好卖,如(g => g.IsBuy == false)不起作用..

Does someone knows what is wrong? 有人知道什么是错的吗?

Great thanks ! 万分谢意 !

If the condition is not satisfied in a Where() LINQ extension method an empty IEnumerable is returned. 如果在Where() LINQ扩展方法中不满足条件,则返回空的IEnumerable So your condition is never met. 所以你的病情永远不会满足。 Use the Any() instead of Where() != null , like so: 使用Any()而不是Where() != null ,如下所示:

@foeach (var item in Model.Where(p => p.user.SellGoods.Any(g => !g.IsBuy)))

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

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