我有两个表,movies并且categories,我首先按类别ID获取有序列表,然后按名称获取.
影片表有三列,ID,Name和CategoryID.类别表2包含列,ID和名称.
我尝试了类似下面的内容,但它没有用.
var movies = _db.Movies.OrderBy( m => { m.CategoryID, m.Name })
Run Code Online (Sandbox Code Playgroud) 我从一个基本类开始,我想使用LINQ在List中操作,如下所示:
public class FooBar
{
public virtual int Id { get; set; }
public virtual string Foo{ get; set; }
public virtual string Bar{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我最终发现使用非lambda LINQ的东西来解决我的问题.
// code somewhere else that works and gets the desired results
var foobarList = GetFooBarList(); // Abstracted out - returns List<Foobar>
// Interesting piece of code that I want to examine
var resultSet = from foobars in foobarList
orderby foobars.Foo, foobars.Bar
select foobars;
// Iterate and do something interesting
foreach …Run Code Online (Sandbox Code Playgroud) 我正在创建一个模拟数据源,我希望能够在SortExpressions列表中传递.
public SortExpression(string name, SortDirection direction)
{
this.name = name;
this.direction = direction;
}
Run Code Online (Sandbox Code Playgroud)
更新与乔恩斯基特的代码,同时也对整个班级.GetData()只是用x个记录填充对象.
public class Data
{
public int Id { get; set; }
public Guid gId { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public DateTime Created { get; set; }
public string SortMe { get; set; }
public static List<Data> GetFakeData(int start, int numberToFetch, IList<SortExpression> sortExpressions, IList<FilterExpression> filterExpressions, …Run Code Online (Sandbox Code Playgroud) 我希望在多个列上应用order by升序,而其他列在LINQ中按降序排列.我怎样才能做到这一点?
我创建了一个具有以下参数的函数:
List<Expression<Func<CatalogProduct, bool>>> orderBy = null
Run Code Online (Sandbox Code Playgroud)
这个参数是可选的,如果它被填充,它应该为我创建一个order by,而不是为我创建一个order,这样我就可以在SQL server上订购结果.
我试过了:
IOrderedQueryable temp = null;
foreach (Expression<Func<CatalogProduct, bool>> func in orderBy)
{
if (temp == null)
{
temp = catalogProducts.OrderBy(func);
}
else
{
temp = temp.ThanBy(func);
}
}
Run Code Online (Sandbox Code Playgroud)
但是比没有重新定义.有人知道我怎么能解决这个问题吗?
我将其更改为.ThenBy()但是只允许直接在.OrderBy()之后,而不是在IOrderedQueryable上
so temp = catalogProducts.OrderBy(func).ThenBy(func); 允许但是temp = catalogProducts.OrderBy(func); temp = temp.ThenBy(func); issn't
还有其他建议吗?
我有3个表小册子,分类和程序.Pamphlet表具有CategoryID和ProgramID列.以下代码有效:
var pamphlets = db.Pamphlets.Include("Category").Include("Program").ToList();
Run Code Online (Sandbox Code Playgroud)
我需要做的是按CategoryName(类别表)排序,然后按PamphletName(小册子表)排序.