Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试从中间层类传回LINQ查询的结果_Linq_Entity Framework 5 - Fatal编程技术网

尝试从中间层类传回LINQ查询的结果

尝试从中间层类传回LINQ查询的结果,linq,entity-framework-5,Linq,Entity Framework 5,我需要一些有关此功能的帮助 第一个查询工作正常: public List<Project> GetProjectByCustomerID(Int16 customerid) { try { using (YeagerTechEntities DbContext = new YeagerTechEntities()) {

我需要一些有关此功能的帮助

第一个查询工作正常:

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;
            }
        }
公共列表GetProjectByCustomerID(Int16 customerid)
{
尝试
{
使用(YeagerTechEntities DbContext=new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled=false;
DbContext.Database.Connection.Open();
IEnumerable project=DbContext.Projects.Where(p=>p.CustomerID==CustomerID);
List myProjects=new List();
myProjects=project.ToList();
归还我的项目;
}
}
捕获(例外情况除外)
{
掷骰子;
}
}
第二个查询有我的项目列表,我只想返回查询中的某些列,但给了我一个错误:“无法将类型IQueryable Anonymoustype#1转换为Generic.List”。设计时编译错误出现在整个SQL语句的“(s=>”前面

公共列表GetProjectByCustomerID(Int16 customerid)
{
尝试
{
使用(YeagerTechEntities DbContext=new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled=false;
DbContext.Database.Connection.Open();
List myProjects=new List();
myProjects=DbContext.Projects.Include(“时间跟踪”)。其中(p=>p.CustomerID==CustomerID&&p.Category.CategoryID==5&&p.Customer.City==“NY”&&p.Status.StatusID==1&&p.Priority.PriorityID==2)。选择(s=>new
{
pridesc=s.Priority.Description,
s、 注意,
statesc=s.Status.Description
});
归还我的项目;
}
}
捕获(例外情况除外)
{
掷骰子;
}
}
第三个查询允许我选择所需的列。整个查询很好,只是我无法传回“project”变量。我得到一个设计时编译错误:“无法将类型Generic.List.AnonymousType#1转换为Generic.List”

公共列表GetProjectByCustomerID(Int16 customerid)
{
尝试
{
使用(YeagerTechEntities DbContext=new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled=false;
DbContext.Database.Connection.Open();
var project=DbContext.Projects.Include(“TimeTrackings”)。其中(p=>p.CustomerID==CustomerID&&p.Category.CategoryID==5&&p.Customer.City==“NY”&&p.Status.StatusID==1&&p.Priority.PriorityID==2)。选择(s=>new
{
pridesc=s.Priority.Description,
s、 注意,
statesc=s.Status.Description
}).ToList();
返回项目;
}
}
捕获(例外情况除外)
{
掷骰子;
}
}
返回第二个和第三个查询的正确方法是什么(语法方面也是如此?


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

您正在
选择
方法中创建匿名类型,而不是
字符串
项目
s.T第一个查询之所以有效,是因为您正在返回一个
列表


如果不需要整个项目,而只需要字段的子集,请创建一个只包含所需字段的新类,并在
Select()中使用该类
而不是创建匿名类型。为了说明这项技术,.

您正在
选择
方法中创建匿名类型,而不是
字符串或
项目
s。第一个查询有效,因为您返回的是
列表


如果不需要整个项目,而只需要字段的子集,请创建一个只包含所需字段的新类,并在
Select()中使用该类
而不是创建匿名类型。为了说明这项技术,.

我上周五试图通过编辑,但没有通过。如果您可以投票支持编辑以显示它,它将显示我尝试此项技术时遇到的错误,因此,最终通过您的反馈解决错误。请不要e用于编辑。我上周五试图通过编辑,但没有通过。如果您可以投票支持编辑以显示它,它将显示我尝试此技术时遇到的错误,因此,最终通过您的反馈解决错误。请投票支持编辑。
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;
            }
        }
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;
            }
        }