So after what felt like a lot of head banging, I have this query:
var widgets = db.Updates
.Where(c => c.Sold.Equals(false))
.GroupBy(c => c.widgetType)
.Select(x => x.OrderByDescending(y => y.TimeStamp).First()).ToList();
widgetGrid.DataSource = widgets;
widgetGrid.DataBind();
Now that I have all the sold widgets I need to add a join, let's say for instance that I want to join the the "Owner" table on ID in Owner equals ID in Widgets and then select Owner.Name and Widget.Style
For the life of me, I seem to be getting nowhere fast... anyone?
As always... I'm deeply grateful for anyone's time in helping me clear out my cobwebs.
答案 0 :(得分:0)
You could do this:
var widgets = db.Updates
.Where(c => !c.Sold)
.GroupBy(c => c.widgetType)
.Select(x => x.OrderByDescending(y => y.TimeStamp).FirstOrDefault());
var result= (from w in widgets
join o in db.Owners on w.OwnerId equals o.Id
select new {o.Name, w.Style}).ToList();
答案 1 :(得分:0)
you may also try:
var widgets = db.Updates
.Where(c => c.Sold.Equals(false))
.GroupBy(c => c.widgetType)
.Select(x => x.OrderByDescending(y => y.TimeStamp).First())
.Join( db.Owners,
u => u.ID,
o => o.ID,
(u, o) => new { o.Name, u.Style }).ToList();
答案 2 :(得分:0)
如果我理解正确,你有两个序列:
并且您想要具有匹配Id的序列和所有者的组合。
顺便说一句,您的小部件可能会有ownerId,或者您的所有者会有一个widgetId,但这不会影响解决方案。
联接如下:
var joinedTable = widgets.join(owners, // join table widgets with table owners
w => w.Id // from widgets take the Id
o => o.Id // from owners also take the Id
(widget, owner) => new // where those Ids match, take the owner and the widget
{ // and take the properties you want
Id = widget.Id,
MyXProperty = owner.X,
MyYProperty = widget.Y,
Widget = widget, // or take the complete widget and owner
Owner = owner,
});
顺便说一句,你写了#34;现在我已经拥有了所有出售的小部件"。从您的代码中我了解到每个更新都有一个布尔属性已售出,并且您希望所有更新在哪里!已售出。我假设你最终得到的产品没有出售?
谓词在where子句中的优点是什么?为什么不呢:
var widgets = db.Updates.Where(c => !c.Sold)
.GroupBy // etc.