Calling Model methods using Linq

·

1 min read

Here is a trick I learned today on how to call methods defined on a model. Basically query the database for the fields in it and convert it to an Enumerable. You can then call the model methods

Example:

var allCoupons = responderCtx.Coupons
    .Where(c => feedbackIds.Contains(c.FeedbackIds))
    .Include( c => c.CouponResponder)
    .AsEnumerable()
    .Select(c => new
    {
        c.CouponResponder.Name,
        c.RedemptionStartDate,
        c.RedemptionEndDate,
        c.RedeemedDate,
        status = c.GetStatus()
    })
    .ToList();