基于 LINQ 中子列表的计数限制对象
由于某种原因,我确实陷入了困境。我有一堆 XML,通过 linq 我已将其调整为适合我的 DTO 对象,这工作正常,但我需要一个额外的过滤器,仅返回具有在一段时间内完全可用的房间的房间类型。
现在,我原来设置 DTO 的查询工作正常,但我想添加一些内容,只返回整个期间有可用房价的房间,所以假设您想预订 10 天,您应该只返回该房间类型有整整 10 天的时间。我原来的查询如下:
var items = (
from rt in data.Descendants("RoomType")
select new RoomType
{
name = rt.Descendants("RoomDescription").Descendants("Text").SingleOrDefault().Value,
rooms = (
from r in rt.Descendants("Room")
select new Room
{
Name = r.Attribute("id").Value,
rates = (
from rr in r.Descendants("RoomRate")
where DateTime.Parse(rr.Attribute("EffectiveDate").Value) >= startDate
where DateTime.Parse(rr.Attribute("EffectiveDate").Value) <= endDate
select new RoomRate
{
EffectiveDate = DateTime.Parse(rr.Attribute("EffectiveDate").Value)
})
})
});
如果在这个查询中完全有可能有限制,那将是令人惊奇的,但我不知道如何做到这一点。
当我尝试在这个查询后面创建另一个查询时,我不知道如何从 RoomType 对象中查询要返回的 Rooms.RoomRates 计数。我尝试过
var result = items.Where(i => i.rooms.Where(r => r.rates.Count() == 10));
,但这给了我一个例外,它无法将 IEnumerable 转换为 bool,.Any() 编译但返回所有内容(可能如预期)。
有谁知道我在这里做错了什么?
编辑:** 这是目前提取数据的方式
房间类型:单人房
- 1(可用天数 10)
- 房间 2(可用天数 10)
房间类型:双床
- 房 3(可用天数 10)
- 房间 4(可用天数) 4)
我想要做的是排除 4 号房间返回,因为它不符合天数标准
,所以我应该返回的是:
房间类型:单人房
- 1(可用天数 10)
- 房间 2 (可用天数 10) 房型:双床
- 房3(可用天数10)
I am well and truly stuck for some reason. I have a bunch of XML which via linq I have adjusted to fit into my DTO objects, this works fine, but I need an additional filter that only returns the Room Types that have rooms that have full availability for a period.
Now my original query to setup the DTO Works fine, but I would like to add something that only returns the rooms that have rates available for the entire periods, so say you want to book 10 days, you should only get the room types back that have the full 10 days available. My original query is the following:
var items = (
from rt in data.Descendants("RoomType")
select new RoomType
{
name = rt.Descendants("RoomDescription").Descendants("Text").SingleOrDefault().Value,
rooms = (
from r in rt.Descendants("Room")
select new Room
{
Name = r.Attribute("id").Value,
rates = (
from rr in r.Descendants("RoomRate")
where DateTime.Parse(rr.Attribute("EffectiveDate").Value) >= startDate
where DateTime.Parse(rr.Attribute("EffectiveDate").Value) <= endDate
select new RoomRate
{
EffectiveDate = DateTime.Parse(rr.Attribute("EffectiveDate").Value)
})
})
});
if it is at all possible to have the restriction in this query that would be amazing, but I couldn't see how to do it.
When I tried to create another query off the back of this one I didn't know how I could query the count of Rooms.RoomRates from the RoomType object to return. I tried
var result = items.Where(i => i.rooms.Where(r => r.rates.Count() == 10));
but that gives me an exception where it can't convert IEnumerable to bool, .Any() compiles but returns everything (as probably expected).
Does anyone know what I am doing wrong here?
EDIT: ** this is how it is pulling the data out at the moment
Room Type: Single
- Room 1 (Days Available 10)
- Room 2 (Days Available 10)
Room Type: Twin
- Room 3 (Days Available 10)
- Room 4 (Days Available 4)
what I am trying to do is exclude Room 4 from returning as it doesn't meet the days criteria
so what I should get back is:
Room Type: Single
- Room 1 (Days Available 10)
- Room 2 (Days Available 10)
Room Type: Twin - Room 3 (Days Available 10)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想要
Room
,您可以展平集合,然后对其进行过滤:如果您想要 RoomType,则需要使用过滤后的
创建新的
:RoomType
对象>房间If you only want
Room
s, you can just flatten the collection, then filter it:If you want RoomTypes, you'll need to create new
RoomType
objects with filteredRooms
: