Reputation: 7818
I want to select certain fields from my domain model, and group by a field within the domain model, and put the results into a ViewModel - however, I can't get the Linq syntax correct.
My Model is:
public class Offer
{
public int OfferId { get; set; }
public string Inclusions { get; set; }
public string Property { get; set; }
public bool IncludeInOffer { get; set; }
public Guid guid { get; set; }
public int NumPeople { get; set; }
public int NumNights { get; set; }
public DateTime ArrivalDate { get; set; }
public string CustomerName { get; set; }
public string EmailAddress { get; set; }
public DateTime OfferCreatedOn { get; set; }
public string Email { get; set; }
public string OfferReference { get; set; }
}
My ViewModel is:
public class OfferVMList
{
public string guid { get; set; }
public int NumPeople { get; set; }
public int NumNights { get; set; }
public DateTime ArrivalDate { get; set; }
public string CustomerName { get; set; }
public string EmailAddress { get; set; }
public DateTime OfferCreatedOn { get; set; }
public string Email { get; set; }
public string OfferReference { get; set; }
}
My Linq is:
OfferVMList item = db.Offers
.GroupBy(x => x.OfferReference)
.Select(ct => new OfferVMList
{
NumPeople = ct.NumPeople
})
.OrderBy(x => x.ArrivalDate);
However, the error message is:
System.Linq.IGrouping<string,FGBS.Models.Offer>' does not contain a definition for 'NumPeople' and no extension method 'NumPeople' accepting a first argument of type 'System.Linq.IGrouping<string,FGBS.Models.Offer>' could be found (are you missing a using directive or an assembly reference?)
'
Can anyone see where I'm going wrong please?
Thank you
Upvotes: 0
Views: 866
Reputation: 5895
Check this:
var item = db.Offers
.OrderBy(x => x.ArrivalDate);
.GroupBy(x => x.OfferReference)
.Select(ct => ct.Select(x => new OfferVMList {
NumPeople = x.NumPeople
});
Upvotes: 3