2010-08-20 88 views
1

我正在使用Linq查詢來填充GridView。然後我將它設置爲DatasourceGridView和Linq查詢排序

在排序事件中,我想要檢索查詢生成的匿名類型並查找成員名稱。

是否可以這樣做?

下面是查詢的爲例

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 
           }; 

然後

gridData.DataSource = q; 

我可以在排序事件retieve匿名類型,做這樣的事情

employeeList.Sort((x, y) => ((Int32)x.GetType().GetProperty(e.SortExpression).GetValue(x, null)).CompareTo((Int32)y.GetType().GetProperty(e.SortExpression).GetValue(y, null)) * sortValue); 
+0

你需要什麼成員名?你能舉一個例子,一些代碼嗎?謝謝! – 2010-08-20 15:25:44

回答

0

你可以用反射來做到這一點,或者你可以使用動態LINQ庫y添加OrderBy子句。

或者,更好的選擇可能是創建一個實際的類/結構來表示您將要檢索的數據。

+0

是的,我已經着手創建一個自定義類,但是我如何使用反射來完成它? 任何事情你可以指點我做?我已經可以使用綁定在表格上的網格來使用列表中的第一個元素。但我不知道我應該做些什麼來獲取這些信息。 有什麼想法? – 2010-08-20 15:13:58