如何使用点语法将实体数据绑定到WebForm控件?

时间:2011-07-01 04:27:18

标签: asp.net entity-framework data-binding

我们已经研究出如何使用“sql like”语法对实体进行数据绑定:

var countryQuery = from c in ShopEntities.ShippingCountries
                   orderby c.Order
                   select new { c.ShippingCountryId, c.Name};

Country.DataValueField = "ShippingCountryId"; //country is a DropDownList
Country.DataTextField = "Name";
Country.DataSource = countryQuery;
DataBind();

但是你如何使用“点”语法

来完成上述操作
var countryQuery = ShopEntities.ShippingCountries.OrderBy(s => s.Order).what to put here????

Country.DataValueField = "ShippingCountryId";
Country.DataTextField = "Name";
Country.DataSource = countryQuery;
DataBind();

2 个答案:

答案 0 :(得分:1)

请参阅LINQ How to select more than 1 property in a lambda expression?

基本上只是:


.Select(c => new { c.ShippingCountryId, c.Name }).ToList();

答案 1 :(得分:1)

与orderBy

的方式相同
ar countryQuery = ShopEntities.ShippingCountries.OrderBy(s => s.Order).Select(s => new classref {ShippingCountryId = s.ShippingCountryId,Name = s.Name})

这一个

ar countryQuery = ShopEntities.ShippingCountries.OrderBy(s => s.Order).Select(s => new { s.ShippingCountryId, s.Name})