简体   繁体   中英

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) :

Here is my request (Model is an IEnumarable from controller, it has correct values) :

 @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..

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. So your condition is never met. Use the Any() instead of Where() != null , like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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