Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用LINQ对通过.Include(“表”)检索的表进行排序?_C#_Asp.net_Linq_Include_Sql Order By - Fatal编程技术网

C# 如何使用LINQ对通过.Include(“表”)检索的表进行排序?

C# 如何使用LINQ对通过.Include(“表”)检索的表进行排序?,c#,asp.net,linq,include,sql-order-by,C#,Asp.net,Linq,Include,Sql Order By,我有这个LINQ代码,它工作得很好。我可以订购帖子,但我还需要订购通过Include(“Comments”)检索的评论:orderby comment.DateAndTime descending 如何在LINQ代码中实现这一点;(订购意见) 我还想知道是否有一种方法可以直接在底层数据库表定义中实现这一点?;注释表定义 public IQueryable<Post> ListViewPosts_GetData() { FacebookDataEntities entities

我有这个LINQ代码,它工作得很好。我可以订购
帖子
,但我还需要订购通过
Include(“Comments”)
检索的
评论:
orderby comment.DateAndTime descending

如何在LINQ代码中实现这一点;(订购意见)

我还想知道是否有一种方法可以直接在底层数据库表定义中实现这一点?;注释表定义

public IQueryable<Post> ListViewPosts_GetData()
{
    FacebookDataEntities entities = new FacebookDataEntities();

    var friends_A = from f in entities.Friends
                    where f.Friend_B == User.Identity.Name && f.AreFriends
                    select f.Friend_A;

    var friends_B = from f in entities.Friends
                    where f.Friend_A == User.Identity.Name && f.AreFriends
                    select f.Friend_B;

    return from p in entities.Posts.Include("Comments")
           let userName = p.UserName
           where userName == User.Identity.Name
          || friends_A.Concat(friends_B).Contains(userName)
           orderby p.DateAndTime descending
           select p;
}
public IQueryable ListViewPosts\u GetData()
{
FacebookDataEntities entities=新的FacebookDataEntities();
var friends\u A=来自实体中的f。friends
其中f.Friend_B==User.Identity.Name&&f.AreFriends
选择f.Friend\u A;
var friends_B=来自实体中的f。friends
其中f.Friend_A==User.Identity.Name&&f.AreFriends
选择f.Friend\u B;
从实体中的p返回。帖子。包括(“评论”)
让userName=p.userName
其中userName==User.Identity.Name
||friends_A.Concat(friends_B).Contains(用户名)
orderby p.DateAndTime降序
选择p;
}

我正在使用手机,因此这可能有错误,但您应该能够执行以下操作:

foreach(var post in p) post.Comments.OrderByDescending(c => c.DateAndTime);

提示一下,你可以在你的数据库中创建一个视图并过滤视图。你可以显示朋友、评论和帖子的表关系吗?包括列吗?也许这是一种错误的方法。。