开发者

Include ENUM description in LINQ results

I have the following GROUP statement producing the results I am looking for.

However, I want to change line 4 below to "group s by s.ComplaintNatureTypeId.ToDescription()" so that the results are grouped by ENUM description rather than a numeric key value.

If I change the line I get the error "Method 'System.String ToDescription(System.Enum)' has no supported translation to SQL."

note: The ToDescription() is a Enum Extension method used to get description from enum.

        var qry = from s in _db.Complaints 
                  where s.Site.SiteDescription.Contains(searchTextSite)
                    && (s.Raised >= startDate && s.Raised <= endDate)
                  group s by s.ComplaintNatureTypeId.ToString()
                      into grp
               开发者_开发技巧       select new
                      {
                          Site = grp.Key,
                          Count = grp.Count()
                      };

        return Json(qry.ToList(), JsonRequestBehavior.AllowGet);

The ENUM class :

using System.ComponentModel;

namespace Emas.Model.Enumerations
{
    public enum ComplaintNatureType
    {
        [Description("- Please Select -")]
        Blank = 0,

        [Description("Letter")]
        LE = 1,

        [Description("eMail")]
        EM = 2,

        [Description("Verbal")]
        VE = 3,

        [Description("Other [see comments]")]
        OT = 4,

    }
}


You'll have to do the .ToDescription() in memory.

var qry = (from s in _db.Complaints 
                  where s.Site.SiteDescription.Contains(searchTextSite)
                    && (s.Raised >= startDate && s.Raised <= endDate)
                  group s by s.ComplaintNatureTypeId
                      into grp
                      select new
                      {
                          Site = grp.Key,
                          Count = grp.Count()
                      })
           .ToList()
           .Select(g => new
                        {
                           Site = g.Site.ToDescription(),
                           g.Count
                        });

 return Json(qry, JsonRequestBehavior.AllowGet);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜