我有这个问题:
public static Object GetEventsByMonth()
{
DataClassesDataContext db = new DataClassesDataContext();
var eventsByMonth = (from e in db.Events
group e by e.StartDate.Value.Month into g
select new { Month = g.Key});
return eventsByMonth;
}
绑定到转发器:
rptMonth.DataSource = EventsQ.GetEventsByMonth();
rptMonth.DataBind();
我无法理解为什么它不让我展示月份:
<%# Eval("Month")%>
它说:System.Web.HttpException: DataBinding: 'Event' does not contain a property with the name 'Month'.
我不想直接显示事件的属性,但我想显示月份......
你能建议一个解决方法吗?
答案 0 :(得分:0)
您不能在创建它的方法之外使用匿名类型,这可能是您遇到此问题的原因。尝试使用创建LINQ对象的相同方法绑定转发器:
DataClassesDataContext db = new DataClassesDataContext();
var eventsByMonth = from e in db.Events
group e by e.StartDate.Value.Month into g
select new
{
Month = g.Key
};
repeater1.DataSource = eventsByMonth;
repater1.DataBind();
你可以做的是创建一个类:
public class SomeClass
{
public int Month { get; set; }
}
var db = new DogDataContext(ConnectString);
var result = from e in db.Events
group e by e.StartDate.Value.Month into g
select new SomeClass()
{
Month = g.Key
};
return result;
答案 1 :(得分:0)
这是我的错: