English 中文(简体)
Linq 退还没有后续行动记录的记录
原标题:Linq to return records that don t have a follow up record

我有这样的疑问,即我们网站上的用户总数为+1:

return db.tblGPlusOneClicks
    .Where(c => 
        c.UserID == UserID
        && c.IsOn
        )
    .Select(c=>c.URLID)
    .Distinct()
    .Count();

数据来源于本表:

“entergraph

单列的URLs,IsOn = 真实的将显示它们拥有的+1 d页数。 然而,该表还储存在<代码>IsOn上的贵重物品。

如果是:

  • Plus one my homepage
  • Unplus one my homepage

It shouldn t count this as a plus for that user in our query as the last action for this URL for this user was to un-plus 1 it. Similarly, if I:

  • Plus one my homepage
  • Unplus one my homepage
  • Plus one my homepage
  • Unplus one my homepage
  • Plus one my homepage

在最初的询问中,它应当把这一点算作,因为对《欧洲统一法》的最后一项行动是加1。

How can I modify my query to count the instances where IsOn is true and that was the last known action for that user for that URL? I m struggling to write a query that does this.

最佳回答

为此:

return db.tblGPlusOneClicks
    .Where(c => c.UserID == UserID)
    .GroupBy(c => c.URLID)
    .Count(g => g.OrderByDescending(c => c.Date).First().IsOn);
问题回答

你们可以这样做:

return (from c in db.tblGPlusOneClicks
        where c.UserID == UserID
        group c by c.URLID into g
        where g.OrderByDescending(x => x.Date).First().IsOn
        select g.Key).Distinct().Count();

2. 对上下层/下层的平衡可能是什么假设:

return db.tblGPlusOneClicks
    .Where(c => c.UserID == UserID)
    .GroupBy(c=>c.URLID)
    .Select(g => new {
          URLID = g.Key,
          VoteBalance = g.Aggregate(0, (a,i) => a+(i.IsOn?1:-1))
    })
    .Sum(u => u.VoteBalance);

这考虑到了以往的所有投票,而不是仅仅看最新记录。 当然,这是你们所希望的。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签