LinqToSQL 返回 IQueryable

LinqToSQL returning IQueryable

我的界面返回 IQueryable。带有模板和 IEnumerable<Agent> 对象的模型 TemplatesAgent。在我的 DataService 中,我正在收集 ListOfAgent 中的所有代理。然后我使用 AgentVersionKey 选择模板内的代理。这是我收到的错误是否与 IQueryable 类型有关?

ExceptionMessage": "Explicit construction of entity type 'OrderTemplateTool.Data.Agent' in query is not allowed.",

界面

IQueryable<TemplatesAgent> GetTemplateAgentNameDiseaseName(string agent, string disease);

数据服务

public IQueryable<TemplatesAgent> GetTemplateAgentNameDiseaseName(string agent, string disease)
{
        //Common 
        var ListOfAgent = (from a in UnitOfWork.GetRepository<Agent>().Get(a => !a.IsDeleted && a.IsCurrentVersion)
                           select new Agent
                           {
                               VersionKey = a.VersionKey,
                               Name = a.Name,
                               RxNormId = a.RxNormId,
                               BrandNames = a.BrandNames
                           });


        var TemplatesAgent = (from t in UnitOfWork.GetRepository<Template>().Get()
                                  join r in UnitOfWork.GetRepository<Regimen>().Get() on t.Id equals r.TemplateId
                                  join rp in UnitOfWork.GetRepository<RegimenPart>().Get() on r.Id equals rp.RegimenId
                                  join re in UnitOfWork.GetRepository<RegimenEntry>().Get() on rp.Id equals re.RegimenPartId
                                  where t.IsCurrentVersion 
                                  && t.Status == 7 
                                  select new TemplatesAgent
                                  {
                                      Template = t,
                                      Agent = ListOfAgent.Where(x => x.VersionKey == re.AgentVersionKey).ToList()
                                  });

型号

public class TemplatesAgent
{
    public Template Template { get; set; }
    public IEnumerable<Agent> Agent { get; set; }
}

控制器

[HttpGet]
public IHttpActionResult TemplatesList(string key, string disease = null, string agent = null)
{
    var result = TemplatesDataService.GetTemplateAgentNameDiseaseName(agent, disease)
        .Where(
            t =>
                t.Template.IsCurrentVersion && t.Template.Status == (short)TemplateMode.Published)
        .OrderBy(t => t.Template.TemplateIdMain)
        .ThenBy(t => t.Template.TemplateIdNumeric)
        .ThenBy(t => t.Template.TemplateIdAlt)
        .ToList()
        .Select(t => new
        {
            TemplateId = t.Template.TemplateId,
            RegimenTitle = t.Template.Title,
            CourseTitle = t.Template.GroupTitle,
            GuidelineTitle = t.Template.GuideLineTitle,
            DiseaseId = t.Template.ExternalDiseaseId,
            DiseaseName = t.Template.DiseaseName
            Agent = t.Agent(a => new
            {
                VersionKey = a.VersionKey,
                Name = a.Name,
                BrandNames = a.BrandNames,
                RxNormId = a.RxNormId
            }).ToList(),
        })
            .Distinct().ToList();

    return Json(result);
}

它告诉你不能在查询中创建 new Agent。试试这个。

    var agentRepo = UnitOfWork.GetRepository<Agent>().Get();
    var ListOfAgent = (from a in agentRepo
                       where !a.IsDeleted && a.IsCurrentVersion
                       select a);


    var TemplatesAgent = (from t in UnitOfWork.GetRepository<Template>().Get()
                              join r in UnitOfWork.GetRepository<Regimen>().Get() on t.Id equals r.TemplateId
                              join rp in UnitOfWork.GetRepository<RegimenPart>().Get() on r.Id equals rp.RegimenId
                              join re in UnitOfWork.GetRepository<RegimenEntry>().Get() on rp.Id equals re.RegimenPartId
                              where t.IsCurrentVersion 
                              && t.Status == 7 
                              select new TemplatesAgent
                              {
                                  Template = t,
                                  Agent = ListOfAgent.Where(x => x.VersionKey == re.AgentVersionKey).ToList()
                              });

控制器

[HttpGet]
public IHttpActionResult TemplatesList(string key, string disease = null, string agent = null)
{
    var result = TemplatesDataService.GetTemplateAgentNameDiseaseName(agent, disease)
        .Where(
            t =>
                t.Template.IsCurrentVersion && t.Template.Status == (short)TemplateMode.Published)
        .OrderBy(t => t.Template.TemplateIdMain)
        .ThenBy(t => t.Template.TemplateIdNumeric)
        .ThenBy(t => t.Template.TemplateIdAlt)
        .Distinct().ToList();

    return Json(result);
}