[英]Join and order in LINQ C#
I have been given the following skeleton code: 我得到了以下基本代码:
var test = SqlCompact(
"OrderID", "ASC", 5, 2,
out count, out sortCriteria, out sql);
This calls the SqlCompact method which performs joins of tables orders, employees and customers & then orders by the inputs eg "ASC" and "Column Name". 这将调用SqlCompact方法,该方法执行表订单,员工和客户的联接,然后通过输入(例如“ ASC”和“列名”)进行订单联接。
I have got the join query working but not sure how to order the results according to the input. 我有加入查询的工作,但不确定如何根据输入排序结果。 This is my code for SqlCompact Method:
这是我的SqlCompact方法的代码:
public List<MyJoin> SqlCompact(
string sort, string sortDir, int rowsPerPage, int page,
out int count, out string sortCriteria, out string sql) {
var cec = new SqlCeConnection(
string.Format(@"Data Source={0}\Northwind.sdf", Path));
var nwd = new DataContext(cec);
nwd.Log = new StringWriter();
var orders = nwd.GetTable<Order>();
var employees = nwd.GetTable<Employee>(); //
var customers = nwd.GetTable<Customer>(); //
count = orders.Count();
sortCriteria = "";
var q = (from od in orders
join em in employees on od.EmployeeID equals em.EmployeeID
join ct in customers on od.CustomerID equals ct.CustomerID
//orderby em.EmployeeID
select new
{
od.OrderID,
od.ShipCountry,
ct.CompanyName,
ct.ContactName,
FullName = em.FirstName + ' '+ em.LastName,
}).ToList();
q.Dump();
sortCriteria: a string composed from sort and sortDir – in the format directly usable by Dynamic LINQ, eg sortCriteria:由sort和sortDir组成的字符串–采用Dynamic LINQ可直接使用的格式,例如
OrderID ASC
ContactName DESC, OrderID DESC
This is what you want to do: if (sort == "OrderId") { q = q.OrderBy(x => x.OrderId); 这就是您要执行的操作:if(sort ==“ OrderId”){q = q.OrderBy(x => x.OrderId); }
}
Passing in the the column name as a string is not a great way to do it though. 不过,将列名作为字符串传递并不是一种好方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.