1
限制LINQ的結果列我有返回晚餐爲GridView控件
public static System.Collections.Generic.List<Dinner> GetDinners()
{
using (DataClassesDataContext h = new DataClassesDataContext())
{
var query = (from dins in h.Dinners
where dins.Title == "New York"
select dins);
return query.ToList();
}
}
我用這個在我的aspx頁面來填充網格在我的業務層,這種靜態方法。
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = BusinessLayer.GetDinners();
GridView1.DataBind();
}
我想限制業務層級的返回列。 我可以在Linq這樣做。
var query = (from dins in h.Dinners
where dins.Title == "New York"
select new { dins.Title, dins.DinnerID });
但後來我得到一個匿名類型的錯誤,這是有道理的,但我該如何解決這個問題?
Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to
'System.Collections.Generic.List<Dinner>
非常感謝 – Hoody