从两个表格中提取内容 [英] Pull content from both tablers
问题描述
您好
这样我必须同时转发用户信息和评论内容,但现在它只会删除页面上显示的用户名。
我在这里做错了什么?
Hello
It is such that I must had both user information and comment content forward, but right now it will only remove the user name appears on the page.
What am I doing wrong here?
var UsersKommentar = from KommentarA in db.forumkommentars
join BrugereA in db.brugeres on KommentarA.brugerid equals BrugereA.Id
where KommentarA.Id == getid
orderby KommentarA.Id descending
select BrugereA;
RepeaterKommentar.DataSource = UsersKommentar;
RepeaterKommentar.DataBind();
EIDT更新程序
EIDT Updater
var UsersKommentar = from KommentarA in db.forumkommentars
join BrugereA in db.brugeres on KommentarA.brugerid equals BrugereA.Id
where KommentarA.Id == getid
orderby KommentarA.Id descending
select new
{
UserName = BrugereA.fornavn + " " + BrugereA.efternavn
};
RepeaterKommentar.DataSource = UsersKommentar;
RepeaterKommentar.DataBind();
<asp:Repeater ID="RepeaterKommentar" runat="server">
<ItemTemplate>
<div class="kommentarBox">
<%--print content here--%>
</div>
</ItemTemplate>
</asp:Repeater>
推荐答案
如果我理解正确,这应该是你想要的:
If I understand correctly, this should be what you want:
var usersKommentar = from KommentarA in db.forumkommentars
join BrugereA in db.brugeres on KommentarA.brugerid equals BrugereA.Id
where KommentarA.Id == getid
orderby KommentarA.Id descending
select new { // change/add property names below as required:
UserName = BrugereA.UserName,
OtherUserInfo = BrugereA.Something,
Kommentar = KommentarA.Kommentar,
Date = KommentarA.Date
};
RepeaterKommentar.DataSource = usersKommentar;
RepeaterKommentar.DataBind();
我们在这里返回一个匿名(编译器生成的)类型,以便从用户和评论中获取列。由于我不知道您的列/属性是如何命名的,我只是在那里写了一些例子。请根据需要更改和添加。
有一件事看起来像是你的疏忽:你是按照KommentarA.Id订购的,也是过滤的KommentarA.Id。其中一条线没有多大意义,或者您打算使用其他一些列进行过滤或订购。
评论后编辑:
另一种方法,如果你没有使用上述工具来处理中继器:
We're returning an anonymous (compiler generated) type here in order to get columns from both Users and Comments. As I don't know how your columns/properties are named, I just wrote some examples there. Please change and add as you need it.
One thing looks to me like it's probably an oversight from you: You're ordering by KommentarA.Id and also filtering by KommentarA.Id. One of those lines doesn't make much sense or maybe you intended to use some other column for filtering or ordering.
Edit after comments:
An alternative way, if you don't get the above to work with the Repeater:
class UserKommentar
{
// change/add properties as required:
public string UserName { get; private set; }
public string Kommentar { get; private set; }
public UserKommentar(string userName, string kommentar)
{
UserName = userName;
Kommentar = kommentar;
}
}
// and then:
var usersKommentar = from KommentarA in db.forumkommentars
join BrugereA in db.brugeres on KommentarA.brugerid equals BrugereA.Id
where KommentarA.Id == getid
orderby KommentarA.Id descending
select new UserKommentar(BrugereA.UserName, KommentarA.Kommentar);
RepeaterKommentar.DataSource = usersKommentar;
RepeaterKommentar.DataBind();
这篇关于从两个表格中提取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!