C# GridView和Linq查询排序
我正在使用C# GridView和Linq查询排序,c#,asp.net,linq,gridview,C#,Asp.net,Linq,Gridview,我正在使用Linq查询来填充GridView 然后我将其设置为数据源 在排序事件中,我希望检索查询生成的匿名类型并查找成员名称 可以这样做吗? 下面是一个查询示例 var q = from inboundCall in dc.GetTable<InboundCall>() join employee in dc.GetTable<Employee>() on inboundCall.EmployeeID equals employee.ID join code in dc
Linq
查询来填充GridView
然后我将其设置为数据源
在排序事件中,我希望检索查询生成的匿名类型并查找成员名称
可以这样做吗?
下面是一个查询示例
var q = from inboundCall in dc.GetTable<InboundCall>()
join employee in dc.GetTable<Employee>() on inboundCall.EmployeeID equals employee.ID
join code in dc.GetTable<Code>() on inboundCall.CodeID equals code.ID
join site in dc.GetTable<Site>() on inboundCall.SiteID equals site.ID
where inboundCall.IsSuccess == true
select new
{
EmployeeNumber = employee.Number,
EmployeeName = employee.Name,
CallerID = inboundCall.CallerID,
SiteName = site.Name,
CallDate = inboundCall.CallDate,
CodeName = code.Name
};
在排序事件中,我可以做什么来重设匿名类型并执行类似操作
employeeList.Sort((x, y) => ((Int32)x.GetType().GetProperty(e.SortExpression).GetValue(x, null)).CompareTo((Int32)y.GetType().GetProperty(e.SortExpression).GetValue(y, null)) * sortValue);
您可以通过反射来实现这一点,也可以使用动态LINQ库来添加OrderBy子句
或者,一个更好的选择可能是创建一个实际的类/结构来表示您将要检索的数据。是的,我已经考虑过创建一个自定义类,但是如何使用反射?你能指点我做什么吗?我已经可以使用绑定在表上的网格,使用列表上的第一个元素来完成。但我不知道我应该做什么样的铸造才能检索到这些信息。知道吗?你需要什么会员名字?你能举个例子,一些代码吗?谢谢
employeeList.Sort((x, y) => ((Int32)x.GetType().GetProperty(e.SortExpression).GetValue(x, null)).CompareTo((Int32)y.GetType().GetProperty(e.SortExpression).GetValue(y, null)) * sortValue);