Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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同时对2个表进行排序_C#_Linq - Fatal编程技术网

C# 使用LinQ同时对2个表进行排序

C# 使用LinQ同时对2个表进行排序,c#,linq,C#,Linq,我的示例包含两个表Book和Comment 书籍: Id Name UserId DateTime B1 Book1 User1 16/11/2016 11:15:00 B2 Book2 User1 16/11/2016 12:15:00 B3 Book3 User2

我的示例包含两个表
Book
Comment

书籍

Id           Name           UserId           DateTime

B1           Book1          User1            16/11/2016 11:15:00
B2           Book2          User1            16/11/2016 12:15:00
B3           Book3          User2            16/11/2016 10:15:00
注释

Id           BookId        UserId            DateTime

C1           B3            User1             16/11/2016 11:17:00
C2           B1            User1             16/11/2016 11:16:00
IEnumerable<Book> comments = _context.Comments.Where(x => x.UserId == userid);
通过特定用户id的
书籍列表

string userid = "User1";

IEnumerable<Book> books = _context.Books.Where(x => x.UserId == userid);
在活动历史记录(网页)中,我想逐一显示我通过
userid
收集的所有
书籍
评论
。但是,如何通过
.OrderByDescending(x=>x.DateTime)
再次对2个对象进行排序

我的目标是:

User1
只需将新书发布到书店即可:

B2           Book2          User1            16/11/2016 12:15:00
B1           Book1          User1            16/11/2016 11:15:00
在此之前,他在自己的帖子上发表了一条评论:

C1           B1            User1             16/11/2016 11:17:00
早些时候,他在另一个线程(
UserId==“User2”
)中发布了一条评论:

年纪大一点的时候,他在店里贴了一本新书:

B2           Book2          User1            16/11/2016 12:15:00
B1           Book1          User1            16/11/2016 11:15:00
我可以通过
DateTime
列(纸上)对它们进行分类:

我怎么分类呢

更新:

我刚刚找到了一个解决办法,但是林克。我想要一个使用LinQ并具有相同结果的解决方案(更短):

namespace-MP
{
公共课堂用书
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
公共字符串用户标识{get;set;}
公共日期时间日期时间{get;set;}
}
公开课评论
{
公共字符串Id{get;set;}
公共字符串BookId{get;set;}
公共字符串用户标识{get;set;}
公共字符串内容{get;set;}
公共日期时间日期时间{get;set;}
}
公开课成绩
{
公共对象对象{get;set;}
公共日期时间日期时间{get;set;}
}
班级计划
{
静态void Main()
{
尝试
{
字符串userid=“User1”;
var books=新列表();
books.Add(新书{Id=“B1”,Name=“Book1”,UserId=“User1”,DateTime=newdatetime(2016,11,16,11,15,00)});
books.Add(新书{Id=“B2”,Name=“Book2”,UserId=“User1”,DateTime=newdatetime(2016,11,16,12,15,00)});
books.Add(新书{Id=“B3”,Name=“Book3”,UserId=“User2”,DateTime=newdatetime(2016,11,16,10,15,00)});
var comments=新列表();
添加(新注释{Id=“c1”,BookId=“B3”,UserId=“User1”,Content=“cmt1”,DateTime=newdatetime(2016,11,16,11,17,00));
添加(新注释{Id=“c2”,BookId=“B1”,UserId=“User1”,Content=“cmt2”,DateTime=newdatetime(2016,11,16,11,16,00));
var result=新列表();
books.ForEach(x=>
{
if(x.UserId==UserId)
{
Add(新结果{Object=x,DateTime=x.DateTime});
}                    
});
comments.ForEach(x=>
{
if(x.UserId==UserId)
{
Add(新结果{Object=x,DateTime=x.DateTime});
}
});
result=result.OrderByDescending(x=>x.DateTime.ToList();
foreach(结果中的var项目)
{
Type Type=item.Object.GetType();
if(type==typeof(MP.Book))
{
var book=(book)item.Object;
Console.WriteLine($“Book:Id:{Book.Id}-Name:{Book.Name}-DateTime:{Book.DateTime}”);
}
if(type==typeof(MP.Comment))
{
var cmt=(Comment)item.Object;
WriteLine($“Comment:Id:{cmt.Id}-Content:{cmt.Content}-DateTime:{cmt.DateTime}”);
}
}
}
捕获(例外e)
{
控制台写入线(e.Message);
}
}
}
}
结果:

请试试这个:

 var vm = from b in Book
          join c in Comment on b.Id equals c.BookId into d
          where b.UserId == userid 
          orderby b.DateTime
          select new {
                      OrderedDate = from item in d
                                    orderby item.DateTime
                                    select item
                     }
或者联合it的另一种方法

var vm = (from a in book
         where a.UserId == userid
         select new { a.DateTime })
         .Union
         (from b in Comment
         where (x=> x.Book.Any(y=>y.UserId == userid))
         select new { b.DateTime })
         .Select(c => new { c.DateTime }).OrderBy(d => d.DateTime);
请试试这个:

 var vm = from b in Book
          join c in Comment on b.Id equals c.BookId into d
          where b.UserId == userid 
          orderby b.DateTime
          select new {
                      OrderedDate = from item in d
                                    orderby item.DateTime
                                    select item
                     }
或者联合it的另一种方法

var vm = (from a in book
         where a.UserId == userid
         select new { a.DateTime })
         .Union
         (from b in Comment
         where (x=> x.Book.Any(y=>y.UserId == userid))
         select new { b.DateTime })
         .Select(c => new { c.DateTime }).OrderBy(d => d.DateTime);

如果我理解正确,您希望在一个网格中显示两个具有不同列的表的记录

要显示哪些列?

假设它们是ID、TYPE(book条目或comment条目)和DT(DateTime)


如果我理解正确,您希望在一个网格中显示两个具有不同列的表的记录

要显示哪些列?

假设它们是ID、TYPE(book条目或comment条目)和DT(DateTime)

我就是这样做的

public class BookAndComment
        {
            public DateTime DateTime { get; set; }
            public Book Book { get; set; }
            public Comment Comment { get; set; }
        }
和查询

var orderedBooksAndComments = = books.Where(book => book.UserId == userId).Select(book =>
            new BookAndComment
            {
                DateTime = book.DateTime,
                Book = book
            }).Union(comments.Where(comment => comment.UserId == userId).Select(comment =>
                new BookAndComment
                {
                    DateTime = comment.DateTime,
                    Comment = comment
                }
            )).OrderByDescending(bookAndComment => bookAndComment.DateTime).ToList();

我就是这样做的

public class BookAndComment
        {
            public DateTime DateTime { get; set; }
            public Book Book { get; set; }
            public Comment Comment { get; set; }
        }
和查询

var orderedBooksAndComments = = books.Where(book => book.UserId == userId).Select(book =>
            new BookAndComment
            {
                DateTime = book.DateTime,
                Book = book
            }).Union(comments.Where(comment => comment.UserId == userId).Select(comment =>
                new BookAndComment
                {
                    DateTime = comment.DateTime,
                    Comment = comment
                }
            )).OrderByDescending(bookAndComment => bookAndComment.DateTime).ToList();


这是一个适合你的解决方案。基本上,我接受了您的想法,将数据对象化并创建一个linq查询,该查询从每个列表中创建一个对象列表,并根据日期时间对它们进行排序。通过重写ToString()方法,打印列表的内容变得非常简单:

public class Book
{
    public const string className = "Book";
    public string Id { get; set; }

    public string Name { get; set; }

    public string UserId { get; set; }

    public DateTime DateTime { get; set; }

    public override string ToString()
    {
        return $"Book:    Id: {Id.PadRight(7)} - Name: {Name.PadRight(14)} - DateTime: {DateTime}";
    }

}

public class Comment
{
    public string Id { get; set; }

    public string BookId { get; set; }

    public string UserId { get; set; }

    public string Content { get; set; }

    public DateTime DateTime { get; set; }

    public override string ToString()
    {
        return $"Comment: Id: {Id.PadRight(7)} - Content: {Content.PadRight(11)} - DateTime: {DateTime}";
    }

}

class Program
{
    static void Main()
    {
        try
        {
            string userid = "User1";

            var books = new List<Book>();
            books.Add(new Book { Id = "B1", Name = "Book1", UserId = "User1", DateTime = new DateTime(2016, 11, 16, 11, 15, 00) });
            books.Add(new Book { Id = "B2", Name = "Book2", UserId = "User1", DateTime = new DateTime(2016, 11, 16, 12, 15, 00) });
            books.Add(new Book { Id = "B3", Name = "Book3", UserId = "User2", DateTime = new DateTime(2016, 11, 16, 10, 15, 00) });

            var comments = new List<Comment>();
            comments.Add(new Comment { Id = "c1", BookId = "B3", UserId = "User1", Content = "cmt1", DateTime = new DateTime(2016, 11, 16, 11, 17, 00) });
            comments.Add(new Comment { Id = "c2", BookId = "B1", UserId = "User1", Content = "cmt2", DateTime = new DateTime(2016, 11, 16, 11, 16, 00) });
            var test = (from b in books
                        where b.UserId == userid
                        select (object)b).Concat
                        (from c in comments
                        where c.UserId == userid
                        select (object)c).OrderBy(x => x.GetType() == typeof(Book)?((Book)x).DateTime:((Comment)x).DateTime);
            foreach(var o in test)
            {
                Console.WriteLine(o);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
创建过滤和排序列表变得简单得多:

string userid = "User1";

var items = new List<Item>();
items.Add(new Book { Id = "B1", Name = "Book1", UserId = "User1", DateTime = new DateTime(2016, 11, 16, 11, 15, 00) });
items.Add(new Book { Id = "B2", Name = "Book2", UserId = "User1", DateTime = new DateTime(2016, 11, 16, 12, 15, 00) });
items.Add(new Book { Id = "B3", Name = "Book3", UserId = "User2", DateTime = new DateTime(2016, 11, 16, 10, 15, 00) });
items.Add(new Comment { Id = "c1", BookId = "B3", UserId = "User1", Content = "cmt1", DateTime = new DateTime(2016, 11, 16, 11, 17, 00) });
items.Add(new Comment { Id = "c2", BookId = "B1", UserId = "User1", Content = "cmt2", DateTime = new DateTime(2016, 11, 16, 11, 16, 00) });
var test = (from b in items
            where b.UserId == userid
            orderby b.DateTime
            select b);
foreach (var o in test)
{
    Console.WriteLine(o);
}
string userid=“User1”;
var items=新列表();
添加(新书{Id=“B1”,Name=“Book1”,UserId=“User1”,DateTime=newdatetime(2016,11,16,11,15,00)});
添加(新书{Id=“B2”,Name=“Book2”,UserId=“User1”,DateTime=newdatetime(2016,11,16,12,15,00)});
添加(新书{Id=“B3”,Name=“Book3”,UserId=“User2”,DateTime=newdatetime(2016,11,16,10,15,00)});
添加(新注释{Id=“c1”,BookId=“B3”,UserId=“User1”,Content=“cmt1”,DateTime=newdatetime(2016,11,16,11,17,00));
添加(新注释{Id=“c2”,BookId=“B1”,UserId=“User1”,Content=“cmt2”,DateTime=newdatetime(2016,11,16,11,16,00));
var测试=(来自项目中的b)
其中b.UserId==UserId
orderby b.DateTime