我在GetForumsByForumGroupID方法中得到了这个错误,它在我的Linq语句中选择了Forum对象。我之前没有遇到过这个错误所以我不知道为什么我有这个错误。我甚至不需要Forum对象我只需要Title和ForumID属性但我不知道如何返回匿名对象,例如
var forums = (from x in db.Forums select new {ForumID = x.ForumID, Title = x.Title}).ToList();
这里有错误。获取由下拉列表传递的选定论坛
public List<Forum> GetForumsByForumGroupID(int ForumGroupID)
{
using (CMSEntities db = new CMSEntities())
{
List<Forum> forums = (from x in db.Forums where x.ParentID == ForumGroupID select
new Forum(x.ForumID, "",DateTime.Now, x.Title, "", 0, x.Importance, false, "",
"", "", DateTime.Now, true, "", 0, "", DateTime.Now, "", 0, 0, "", 0)).ToList<Forum>();
return forums;
}
}
标记:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" AutoPostBack="true"
DataSourceID="ObjectDataSource1" DataTextField="Key"
DataValueField="Value">
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetDisplayForumGroups" TypeName="CMS.Framework.Forums">
</asp:ObjectDataSource>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource2">
<Columns>
<asp:BoundField DataField="ForumID" HeaderText="ForumID"
SortExpression="ForumID" />
<asp:BoundField DataField="AddedBy" HeaderText="AddedBy"
SortExpression="AddedBy" />
<asp:BoundField DataField="AddedDate" HeaderText="AddedDate"
SortExpression="AddedDate" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="ParentID" HeaderText="ParentID"
SortExpression="ParentID" />
<asp:BoundField DataField="Importance" HeaderText="Importance"
SortExpression="Importance" />
<asp:CheckBoxField DataField="Moderated" HeaderText="Moderated"
SortExpression="Moderated" />
<asp:BoundField DataField="Url" HeaderText="Url" SortExpression="Url" />
<asp:BoundField DataField="ImageUrl" HeaderText="ImageUrl"
SortExpression="ImageUrl" />
<asp:BoundField DataField="UpdatedBy" HeaderText="UpdatedBy"
SortExpression="UpdatedBy" />
<asp:BoundField DataField="UpdatedDate" HeaderText="UpdatedDate"
SortExpression="UpdatedDate" />
<asp:CheckBoxField DataField="Active" HeaderText="Active"
SortExpression="Active" />
<asp:BoundField DataField="ForumGroup" HeaderText="ForumGroup"
SortExpression="ForumGroup" />
<asp:BoundField DataField="ThreadCount" HeaderText="ThreadCount"
SortExpression="ThreadCount" />
<asp:BoundField DataField="LastPostBy" HeaderText="LastPostBy"
SortExpression="LastPostBy" />
<asp:BoundField DataField="LastPostDate" HeaderText="LastPostDate"
SortExpression="LastPostDate" />
<asp:BoundField DataField="LastPostTitle" HeaderText="LastPostTitle"
SortExpression="LastPostTitle" />
<asp:BoundField DataField="LastPostID" HeaderText="LastPostID"
SortExpression="LastPostID" />
<asp:BoundField DataField="ThreadID" HeaderText="ThreadID"
SortExpression="ThreadID" />
<asp:BoundField DataField="ThreadTitle" HeaderText="ThreadTitle"
SortExpression="ThreadTitle" />
<asp:BoundField DataField="ReplyCount" HeaderText="ReplyCount"
SortExpression="ReplyCount" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
SelectMethod="GetForumsByForumGroupID" TypeName="CMS.Framework.Forums">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedValue"
Type="Int32" Name="ForumGroupID" />
</SelectParameters>
</asp:ObjectDataSource>
更新:
public GetForumsByForumGroupID(int ForumGroupID)
{
using (CMSEntities db = new CMSEntities())
{
var forums = (from x in db.Forums where x.ParentID == ForumGroupID select
x).ToList();
return forums;
}
}
不能这样做
public List<Forum> GetForumsByForumGroupID(int ForumGroupID)
{
using (CMSEntities db = new CMSEntities())
{
List<Forum> formus = (from x in db.Forums where x.ParentID == ForumGroupID select
x).ToList<Forum>();
return forums;
}
}
答案 0 :(得分:1)
为什么要首先创建新的Forum
个对象?除非有充分理由不这样做,否则只需select x
。