LINQ一个简单的问题

时间:2011-06-15 13:05:30

标签: c# linq

我有那个班:

public class Post 
{
    public Oid Id { get; private set; }

    public IList<Comment> Comments { get; set; }
}

public class Comment
{
    public Guid Id { get;  set; }
    public DateTime TimePosted { get; set; }
}

如何在linq中选择评论有的帖子,例如TimePosted&gt; = DateTime.Now ??

2 个答案:

答案 0 :(得分:5)

给定一组名为“Post”的posts个对象,您可以执行以下操作:

var result = posts.Where(p => p.Comments.Any(c => c.TimePosted >= DateTime.Now));

请注意,这是Method语法的示例。有关查询语法的示例,请参阅cybernate's answer

MSDN对这里提供的两种样式进行了比较:LINQ Query Syntax versus Method Syntax

答案 1 :(得分:2)

试试这个:

from p in context.Posts
from c in p.Comments
where c.TimePosted >= DateTime.Now
select p