1

I am querying an EF entity MatchHistory:

   public partial class MatchHistory
    {
        public System.Guid ApplicantId { get; set; }
        public int BuyerId { get; set; }
        public System.DateTime AppliedOn { get; set; }
        public int MatchResultId { get; set; }
        public System.DateTime ReapplyOn { get; set; }

        public virtual MatchBuyer MatchBuyer { get; set; }
    }

I currently have this linq statement in my code.

            return r.Find()
                .Where(x => x.AppliedOn > cutoff && x.MatchResultId == (int)MatchResult.Accepted)
                .ToList();

This will return all rows of the type MatchHistory matching the criteria.

However, what I want to do is group by BuyerId and return a count by BuyerId.

Here's the class, I want to output to:

public class QuotaCount
{
    public int BuyerId { get; set; }
    public int Count { get; set; }
}

Haven't quite managed to get the right syntax together yet - any advice appreciated.

4

2 に答える 2

2
return r.Find()
        .Where(x => x.AppliedOn > cutoff && x.MatchResultId == (int)MatchResult.Accepted)
        .GroupBy(x => x.BuyerId)
        .Select(x => new QuotaCount { BuyerId = x.Key, Count = x.Count() })
        .ToList();
于 2013-06-19T09:43:46.460 に答える
1
return r.Find()
                .Where(x => x.AppliedOn > cutoff && x.MatchResultId == (int)MatchResult.Accepted)
                .GroupBy(mh=>mh.BuyerId).Select(gr=>new QuotaCount{BuyerId=gr.Key,Count=gr.Count});
于 2013-06-19T09:44:43.250 に答える