LINQ A simple question
I have that class:
public class Post
{
public Oid Id { get; private set; }
public IList<Comment> Comments { get; set; }
}
public class Comment
{
public Guid Id { get; 开发者_JAVA百科set; }
public DateTime TimePosted { get; set; }
}
how in linq select that posts whose comments has e.g TimePosted >= DateTime.Now ??
Given a collection of Post
objects called "posts
", you'd do something like this:
var result = posts.Where(p => p.Comments.Any(c => c.TimePosted >= DateTime.Now));
Note that this is an example of Method syntax. For an example of Query syntax, see cybernate's answer.
MSDN has a comparison of the two styles available here: LINQ Query Syntax versus Method Syntax.
Try this:
from p in context.Posts
from c in p.Comments
where c.TimePosted >= DateTime.Now
select p
精彩评论