【问题标题】:Trying to pass back the results of a LINQ query from a middle tier class试图从中间层类传回 LINQ 查询的结果
【发布时间】:2013-07-11 00:23:00
【问题描述】:

我需要有关此功能的帮助...

第一个查询运行良好:

public List<Project> GetProjectByCustomerID(Int16 customerid)
        {
            try
            {
                using (YeagerTechEntities DbContext = new YeagerTechEntities())
                {
                    DbContext.Configuration.ProxyCreationEnabled = false;
                    DbContext.Database.Connection.Open();

                    IEnumerable<Project> project = DbContext.Projects.Where(p => p.CustomerID == customerid);

                    List<Project> myProjects = new List<Project>();

                    myProjects = project.ToList();

                    return myProjects;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

第二个查询有我的项目列表,我只想返回查询中的某些列,但给了我错误:“无法将类型 IQueryable Anonymoustype#1 转换为 Generic.List”。设计时编译错误出现在 "(s =>"

之前的整个 SQL 语句上
public List<Project> GetProjectByCustomerID(Int16 customerid)
        {
            try
            {
                using (YeagerTechEntities DbContext = new YeagerTechEntities())
                {
                    DbContext.Configuration.ProxyCreationEnabled = false;
                    DbContext.Database.Connection.Open();

                    List<Project> myProjects = new List<Project>();

                    myProjects = DbContext.Projects.Include("TimeTrackings").Where(p => p.CustomerID == customerid && p.Category.CategoryID == 5 && p.Customer.City == "NY" && p.Status.StatusID == 1 && p.Priority.PriorityID == 2).Select(s => new
                    {
                        pridesc = s.Priority.Description,
                        s.Notes,
                        stadesc = s.Status.Description
                    });

                    return myProjects;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

第三个查询允许我选择我需要的列。整个查询都很好,除了我不能传回“项目”变量。我收到设计时编译错误:“无法将类型 Generic.List.AnonymousType#1 转换为 Generic.List”

public List<String> GetProjectByCustomerID(Int16 customerid)
        {
            try
            {
                using (YeagerTechEntities DbContext = new YeagerTechEntities())
                {
                    DbContext.Configuration.ProxyCreationEnabled = false;
                    DbContext.Database.Connection.Open();

                    var project = DbContext.Projects.Include("TimeTrackings").Where(p => p.CustomerID == customerid && p.Category.CategoryID == 5 && p.Customer.City == "NY" && p.Status.StatusID == 1 && p.Priority.PriorityID == 2).Select(s => new
                    {
                        pridesc = s.Priority.Description,
                        s.Notes,
                        stadesc = s.Status.Description
                    }).ToList();

                    return project;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

传回第二个和第三个查询的正确方法是什么(语法方面也是如此)?

我知道我可以直接在代码隐藏中执行第三个查询,并将其绑定到以“var”变量作为数据源的网格。但是,如果有人能告诉我如何成功地将第二和第三个查询类型从中间层类传回前端,我将不胜感激。

【问题讨论】:

    标签: linq entity-framework-5


    【解决方案1】:

    您在Select 方法中创建匿名类型,而不是strings 或Projects。第一个查询有效,因为您返回的是 List&lt;Project&gt;

    如果您不想要整个项目,而只想要字段的子集,请创建一个仅包含您需要的字段的新类,并在您的 Select() 中使用它,而不是创建匿名类型。有关该技术的说明,请see here

    【讨论】:

    • 我试图在上周五进行编辑,但没有通过。如果您可以为编辑投票以显示它,它将显示我在尝试此技术时遇到的错误,因此,最终通过您的反馈解决错误。请为修改投票。
    猜你喜欢
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多