首页 新闻 会员 周边 捐助

linq to sql 怎么实现动态排序?

0
[已关闭问题]
INQ TO SQL 如何实现动态排序?<br>比如我把LINQ TO SQL 获取的结果集绑定到ListView,然后点击ListView的某一列来排序。怎么实现?
FrancisLiu的主页 FrancisLiu | 初学一级 | 园豆:200
提问于:2008-03-22 09:14
< >
分享
其他回答(1)
0
这个例子是说使用动态查询,查询London的顾客并按ContactName排序,关于ListView控件使用参看MSDN,希望对你有帮助。 ParameterExpression param = Expression.Parameter(typeof(Customer), "c"); Expression left = Expression.Property(param, typeof (Customer).GetProperty("City")); Expression right = Expression.Constant("London"); Expression filter = Expression.Equal(left, right); Expression pred = Expression.Lambda(filter, param); IQueryable custs = db.Customers; Expression expr = Expression.Call(typeof(Queryable), "Where", new Type[] { typeof(Customer) }, Expression.Constant(custs), pred); expr = Expression.Call(typeof(Queryable), "OrderBy", new Type[] { typeof(Customer), typeof(string) }, custs.Expression, Expression.Lambda(Expression.Property(param, "ContactName"), param)); IQueryable<Customer> query = db.Customers.AsQueryable(). Provider.CreateQuery<Customer>(expr);
李永京 | 园豆:3114 (老鸟四级) | 2008-03-22 12:35
0
楼上的好复杂,你可以这样写 var q= from p in db.products select p; switch(orderyname){ case "id": q= q.OrderBy(t=>t.id); //或者q= q.OrderByDescending(t=>t.id)//倒序 break; case "name" q= q.OrderBy(t=>t.name); //或者q= q.OrderByDescending(t=>t.name)//倒序 break; }
永不言败 | 园豆:350 (菜鸟二级) | 2008-04-01 16:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册