简体   繁体   English

使用LINQ2SQL与多个表进行GridView排序

[英]GridView Sort using LINQ2SQL with multiple tables

I'm trying to sort a GridView using LINQ2SQL with multiple tables. 我正在尝试使用具有多个表的LINQ2SQL对GridView进行排序。 Since the columns in the grid contain data from several tables how do I tell LINQ which column name to sort by. 由于网格中的列包含来自多个表的数据,我如何告诉LINQ排序哪个列名。

For example the user clicks on the first column ( POCNum ) which is in table p so I would use p.POCNum to orderby . 例如,用户点击了第一列( POCNum ),其是表p所以我会使用p.POCNumorderby Problem is e.SortExpressio n only contains the word POCNum . 问题是e.SortExpressio n仅包含单词POCNum No way to link this back to table p . 无法将其链接回表p So if somebody clicks on column Contact_ID which is in table c I need to orderby c.Contact_ID in the LINQ query. 因此,如果有人单击表c Contact_ID列,则需要按LINQ查询中的orderby c.Contact_ID

Any suggestions would be appreciated. 任何建议,将不胜感激。

protected void GridView1_Sort(Object sender, GridViewSortEventArgs e)
{
     DataClasses1DataContext db = new DataClasses1DataContext();
     var results = (from p in db.POCs<BR/>
                   join c in db.Contacts on p.POC_ID equals c.POC_ID
                   join a in db.Auxilaries on p.POC_ID equals a.POC_ID
                   join l in db.Legends on p.Basis equals l.Legend_code_value
                   orderby e.SortExpression // problem - 
                   //e.SortExpression does not contain table reference         
                   where p.DebtorObj_ID == Convert.ToInt32(ddlDebtor.SelectedItem.Value)
                   select new { p.POC_ID, p.POC_Date, p.POCNum, p.Total_Amt, p.Secure_Amt,
                                a.AMailing_Name1, l.Legend_Description }).ToList();
            GridView1.DataSource = results;
            GridView1.DataBind();
}

What I'd be looking at is either expressions or dynamic linq. 我要查看的是表达式或动态linq。

If you see the Generic Sorter at http://www.singingeels.com/Articles/Self_Sorting_GridView_with_LINQ_Expression_Trees.aspx or approach in http://devtoolshed.com/content/gridview-objectdatasource-linq-paging-and-sorting that should help. 如果您在http://www.singingeels.com/Articles/Self_Sorting_GridView_with_LINQ_Expression_Trees.aspx上看到了通用排序器,或者在http://devtoolshed.com/content/gridview-objectdatasource-linq-paging-and-sorting中找到了帮助。

For dynamic linq see Is there a pattern using Linq to dynamically create a filter? 有关动态linq的信息,请参阅是否存在使用Linq动态创建过滤器的模式? which is about filters but the same applies for ordering. 这与过滤器有关,但订购时也是如此。 Note that your anonymous type doesn't have duplicate column names so the multiple table source shouldn't matter 请注意,您的匿名类型没有重复的列名,因此多表源无关紧要

Following the example within the generic sorter, within the Sorting handler for a GridView (explicit implementation, not your LINQ query but that shouldn't matter): 遵循通用排序器中的示例,在GridView的Sorting处理程序中(显式实现,不是您的LINQ查询,但这无关紧要):

IEnumerable<CustomerOrder> customerOrders = (LINQ statement)  
var param = Expression.Parameter = typeof(CustomerOrder));  
var sortExpression = Expression.Lambda<Func<CustomerOrder, object>>(Expression.Convert(Expression.Property(param,e.SortExpression), typeof(object), param);  

MyGridView.DataSource = customerOrders.AsQueryable().OrderBy(sortExpression);  
MyGridView.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM