使用 Linq 和 lambda 展平列表
我有课。
public class MedicalRequest
{
private int id
private IList<MedicalDays> Days
private string MedicalUser
...
}
另一个
public class MedicalDays
{
private int id;
private DateTime? day
private MedicalRequest request
...
}
我有 MedicalUser,所以我能够选择一个
IList<MedicalRequest> reqList = dao.FindAll(example);
我现在想做的就是展平 MedicalDays 列表并返回 DateTime 天。
比如
IList<DateTime> dateList = reqList.SelectMany(i => i.MedicalDays.day);
有人可以推动我朝正确的方向前进吗?
感谢您抽出时间。
I have a class.
public class MedicalRequest
{
private int id
private IList<MedicalDays> Days
private string MedicalUser
...
}
and another
public class MedicalDays
{
private int id;
private DateTime? day
private MedicalRequest request
...
}
I have the MedicalUser so I'm able to select an
IList<MedicalRequest> reqList = dao.FindAll(example);
What I would love to be able to do at this point is flatten out the Lists of MedicalDays and return the DateTime day.
Something like
IList<DateTime> dateList = reqList.SelectMany(i => i.MedicalDays.day);
Can someone give me a push in the right direction?
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您即将完成:
或者:
IList
而不是IEnumerable
,您可以调用ToList()
结果如果您需要使用
DateTime
而不是DateTime?
,您可以像这样过滤掉空日期:You're nearly there:
Or:
IList<T>
instead ofIEnumerable<T>
you can callToList()
on the resultIf you need to work with
DateTime
instead ofDateTime?
you can filter out null days like this: