我遇到一个问题,我有一个表Book
与BookByEditor
表的多对多关系,这个表与Editor
表具有相同的关系,这个想法是一本书可能有很多编辑,编辑可能会有很多书。
这里快速了解我的表格:
public partial class Book
{
public int BookID { get; set; }
public string BookName { get; set; }
(...)
public virtual ICollection<BookByEditor> BookByEditor { get; set; }
}
public partial class BookByEditor
{
public int BookByEditorId { get; set; }
public int BookID { get; set; }
public int EditorID { get; set; }
public virtual Book Book { get; set; }
public virtual Editor Editor { get; set; }
}
public partial class Editor
{
public int EditorID { get; set; }
public string EditorName { get; set; }
(...)
public virtual ICollection<BookByEditor> BookByEditor { get; set; }
}
所以我喜欢的是,例如,能够进行查询以获得所有书籍按编辑按字母顺序排序;意思是编辑A,编辑B,编辑C等。我希望我对自己所寻求的内容足够清楚。
我正在使用linq来实现sql和linqkit,所以任何方法都可以。
通常我会这样做:
IQueryable<Editor> editors = from e in context.Editor.OrderBy(x => x.EditorName)
select e;
List<Editor> listEditors = new List<Editor>
{
editors.First()
};
count = 0;
Editor editorModel = listEditors.Last();
while (listBook.Count < 20)
{
IQueryable<Book> bookByEditor =
from e in context.Book.Where(
item => item.BookByEditor.Any(x => x.EditorID == editorModel.EditorID))
select e;
switch (secondOption)
{
default:
fetch = bookByEditor.OrderBy(x => x.BookID).Skip(pageCount * 20).Take(20).ToList();
break;
case "New":
fetch = bookByEditor.OrderByDescending(x => x.DateAdded).Skip(pageCount * 20).Take(20).ToList();
break;
case "Old":
fetch = bookByEditor.OrderBy(x => x.DateAdded).Skip(pageCount * 20).Take(20).ToList();
break;
}
listBook.AddRange(fetch);
count++;
listEditors.AddRange(editors.Skip(count).Take(1));
editorModel = listEditors.Last();
}
但我对这种方法不满意,我不确定结果......
答案 0 :(得分:0)
如何通过编辑器对书籍进行分组,然后对其进行排序。选择多少来取回书籍?