我需要有关此功能的帮助...
第一个查询工作正常:
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”变量作为数据源。但是,如果有人能告诉我如何成功地将第二和第三个查询类型从中间层类传递回前端,我将不胜感激。