I’m trying to sort a GridView using LINQ2SQL with multiple tables. Since the columns in the grid contain data from several tables how do I tell LINQ which column name to sort by.
For example the user clicks on the first column (POCNum
) which is in table p
so I would use p.POCNum
to orderby
. Problem is e.SortExpressio
n only contains the word POCNum
. No way to link this back to table 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.
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();
}