C#:返回符合条件的任何项目 [英] C#: Return any item that matches the condition

查看:156
本文介绍了C#:返回符合条件的任何项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个方法:

    public ActionResult ShowAvailableSpots(int Id, DateTime ArrivalDate, DateTime LeaveDate)
    {
        var query2 = db.Spots
            .Where(c => db.Reservations.Any(r =>
                           DbFunctions.TruncateTime(ArrivalDate) <= DbFunctions.TruncateTime(r.ArrivalDate) && DbFunctions.TruncateTime(LeaveDate) <= DbFunctions.TruncateTime(r.ArrivalDate)
                        || DbFunctions.TruncateTime(ArrivalDate) >= DbFunctions.TruncateTime(r.LeaveDate)
            )).ToList();

        ViewBag.StartingDate = ArrivalDate;
        ViewBag.EndingDate = LeaveDate;
        ViewBag.AvailableSpots = query2;

        ViewBag.CampingSpotId = new SelectList(query2, "CampingSpotId", "SpotName");

        return View();
    }

它决定羯羊任何的保留匹配日期条件。如果它们不匹配,则返回用Campingspots列表

It determines wether any of the reservations match the date criteria. If they don't match, then the list with Campingspots is returned.

的问题是,它返回所有斑点或NONE掩护,而不只是可用的斑点。这是由于。任何方法。我怎样才能筛选出所不具备的campingspots?

The problem is, that it is returning ALL spots or NONE spots instead of just the spots that are available. This is due to the .Any method. How can I filter out the campingspots that are not available?

推荐答案

尝试是这样的:

var query2 = db.Spots.Where(c => db.Reservations
                                   .Where(r => c.CampingSpotId == r.CampingSpotId)
                                   .All(r => DbFunctions.TruncateTime(LeaveDate) <= DbFunctions.TruncateTime(r.ArrivalDate)
                                          || DbFunctions.TruncateTime(ArrivalDate) >= DbFunctions.TruncateTime(r.LeaveDate))
                   )).ToList();

其中,声明说,我们只检查适用于露营点的保留,以及所有语句检查,以确保该营地每个预订我们感兴趣的窗外。

The inner Where statement says we're only checking the reservations that apply to that camping spot, and the All statement checks to make sure that every reservation for that campsite is outside the window we're interested in.

这篇关于C#:返回符合条件的任何项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆