我有一个帐户类型:
Account:
String Name
List<Group> Groups
组类型为
Group:
List<String> Addresses
List<Schedule> Schedules
并且计划类型是
Schedule:
String Type
String Time
我想查找所有与时间匹配的时间表,并将帐户名称和所有地址一起发送,以及时间表类型:
SendMail(AccountName, Type, Addresses)
所以我为RavenDB编写了这个查询,其中所有这些信息都存储为单个文档:
var accounts = session.Query<Account>().Where(
a => a.Groups.Any(
g=> g.Schedules.Any(s => s.Time== timeString)
)
);
然后处理它:
foreach (Account account in accounts) {
List<String> addressesToNotify = new List<String>();
List<String> addressesToAdvertise = new List<String>();
foreach (Group group in account.Groups) {
foreach (Schedule schedule in group .Schedules) {
if (schedule .Time== timeString) {
if (schedule.Type== "Notify") {
addressesToNotify.AddRange(group.Addresses);
} else {
addressesToAdvertise .AddRange(group.Addresses);
}
}
}
}
SendMail(account.Name, "Notify", addressesToNotify);
SendMail(account.Name, "Advertise", addressesToAdvertise);
}
但是我有两个问题。
1)RavenDB无法理解where里面的任何内容
Could not understand expression: a => a.Groups.Any(
g=> g.Schedules.Any(s => s.Time== timeString)
)
2)我觉得那些嵌套循环非常低效,而某些Select会使它更快更可读
如何修复查询并使处理代码更具可读性?