使用 Linq 和 lambda 展平列表

发布于 2024-09-11 17:44:44 字数 654 浏览 5 评论 0原文

我有课。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗喜 2024-09-18 17:44:44

您即将完成:

IEnumerable<DateTime?> dateList = reqList.SelectMany(i => i.MedicalDays)
                                         .Select(m => m.day);

或者:

IEnumerable<DateTime?> dateList = reqList.SelectMany(i => i.MedicalDays,
                                                     (i, m) => m.day);
  • 如果您需要 IList 而不是 IEnumerable,您可以调用 ToList()结果
  • 如果您需要使用 DateTime 而不是 DateTime?,您可以像这样过滤掉空日期:

    IEnumerable<日期时间?> dateList = reqList.SelectMany(i => i.MedicalDays)
                                             .Select(m => m.day)
                                             .Where(x => x.HasValue)
                                             .Select(x => x.Value);
    

You're nearly there:

IEnumerable<DateTime?> dateList = reqList.SelectMany(i => i.MedicalDays)
                                         .Select(m => m.day);

Or:

IEnumerable<DateTime?> dateList = reqList.SelectMany(i => i.MedicalDays,
                                                     (i, m) => m.day);
  • If you need an IList<T> instead of IEnumerable<T> you can call ToList() on the result
  • If you need to work with DateTime instead of DateTime? you can filter out null days like this:

    IEnumerable<DateTime?> dateList = reqList.SelectMany(i => i.MedicalDays)
                                             .Select(m => m.day)
                                             .Where(x => x.HasValue)
                                             .Select(x => x.Value);
    
海螺姑娘 2024-09-18 17:44:44
IEnumerable<DateTime> dateList = reqList.SelectMany(i => i.MedicalDays)
                                        .Select(i => i.day);
IEnumerable<DateTime> dateList = reqList.SelectMany(i => i.MedicalDays)
                                        .Select(i => i.day);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文