我收到了以下骨架代码:
var test = SqlCompact(
"OrderID", "ASC", 5, 2,
out count, out sortCriteria, out sql);
这将调用SqlCompact方法,该方法执行表命令,员工和客户的联接。然后通过输入命令,例如“ASC”和“列名”。
我已经使用了连接查询,但不确定如何根据输入对结果进行排序。这是我的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:由sort和sortDir组成的字符串 - 采用Dynamic LINQ可直接使用的格式,例如:
OrderID ASC
ContactName DESC, OrderID DESC
答案 0 :(得分:0)
这是你想要做的: if(sort ==&#34; OrderId&#34;){ q = q.OrderBy(x =&gt; x.OrderId); }
将列名称作为字符串传递并不是一种很好的方法。