我已经浏览过StackOverlow,但未能就此找到明确的答案。
下面我有一个我目前拥有的代码片段,我将解释我想要实现的目标。
Table<Gallery> galleries = pdc.GetTable<Gallery>();
Table<GalleryImage> images = pdc.GetTable<GalleryImage>();
Table<Comment> comments = pdc.GetTable<Comment>();
var query = from gallery in galleries
join image in images on gallery.id equals image.galleryid into joinedimages
join comment in comments on gallery.id equals comment.galleryid into joinedcomments
select gallery;
gallst.DataSource = query;
gallst.DataBind();
从上面我有以下转发器:
<asp:Repeater ID="gallst" runat="server" EnableViewState="false">
<HeaderTemplate>
<div id="gallery">
</HeaderTemplate>
<ItemTemplate>
<div class="item">
<h2><%# DataBinder.Eval(Container.DataItem, "name") %> @ <%# DataBinder.Eval(Container.DataItem, "wheretaken") %></h2>
<ul class="images">
<asp:Repeater ID="galimgs" runat="server" EnableViewState="false" DataSource='<%# Eval("GalleryImages") %>'>
<ItemTemplate>
<li><a href="<%# DataBinder.Eval(Container.DataItem, "image") %>.jpg" title="<%# DataBinder.Eval(((System.Web.UI.WebControls.RepeaterItem)Container.Parent.Parent).DataItem, "name") %>" rel="prettyPhoto[<%# DataBinder.Eval(Container.DataItem, "galleryid")%>]" class="thickbox"><img src="<%# DataBinder.Eval(Container.DataItem, "image") %>_thumb.jpg" /></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
<div class="comments">
<asp:Repeater ID="galcomments" runat="server" EnableViewState="false" DataSource='<%# Eval("Comments") %>'>
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><%# GetUserName(new Guid(Eval("userid").ToString())) %> said: <%#DataBinder.Eval(Container.DataItem, "comment1") %> (<%# DataBinder.Eval(Container.DataItem, "date", "{0:dddd MM, yyyy hh:mm tt}") %>)</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
<uc:makecomment ID="mcomment" runat="server" PhotoID='<%# DataBinder.Eval(Container.DataItem, "id") %>'></uc:makecomment>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>
我想做的(理想情况下)只是为每个画廊拍摄前3个评论。
我尝试了以下LINQ查询但没有运气:
var query = from gallery in galleries
join image in images on gallery.id equals image.galleryid into joinedimages
join comment in comments.Take(3) on gallery.id equals comment.galleryid into joinedcomments
select gallery;
有没有人对如何实现这一点有任何建议?
答案 0 :(得分:1)
这看起来可能是您需要的调整。它来自一个非常有用的LINQ示例站点。
此sample打印华盛顿客户的前三个订单的客户ID,订单ID和订单日期。该示例使用Take将查询表达式生成的序列限制为前三个订单。
public void Linq21() {
List<Customer> customers = GetCustomerList();
var first3WAOrders = (
from c in customers
from o in c.Orders
where c.Region == "WA"
select new {c.CustomerID, o.OrderID, o.OrderDate} )
.Take(3);
Console.WriteLine("First 3 orders in WA:");
foreach (var order in first3WAOrders) {
ObjectDumper.Write(order);
}
}
结果
西澳州的前3个订单:
CustomerID=LAZYK OrderID=10482 OrderDate=3/21/1997
CustomerID=LAZYK OrderID=10545 OrderDate=5/22/1997
CustomerID=TRAIH OrderID=10574 OrderDate=6/19/1997
答案 1 :(得分:0)
我设法让它与之合作:
Table<Gallery> galleries = pdc.GetTable<Gallery>();
Table<GalleryImage> images = pdc.GetTable<GalleryImage>();
Table<Comment> comments = pdc.GetTable<Comment>();
var query = from gallery in galleries
join image in images on gallery.id equals image.galleryid into joinedimages
join comment in comments on gallery.id equals comment.galleryid into joinedcomments
select new
{
name = gallery.name,
wheretaken = gallery.wheretaken,
id = gallery.id,
GalleryImages = joinedimages,
Comments = joinedcomments.Take(3)
};
gallst.DataSource = query;
gallst.DataBind();
随着选择的发生。谢谢大家的帮助。关于如何写这个“更好”的任何建议都将不胜感激。