需要一些帮助来破译从位于中间层类的LINQ查询中传回数据的正确方法......
我可以正常使用第一个查询,但我无法从父实体或Project实体中选择任何特定列。
public List<Project> GetProjects()
{
try
{
using (YeagerTechEntities DbContext = new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
IEnumerable<Project> project = DbContext.Projects.Where(p => p.ProjectID > 0);
List<Project> myProjects = new List<Project>();
myProjects = project.ToList();
return myProjects;
}
}
catch (Exception ex)
{
throw ex;
}
}
我无法使用第二个查询,因为我在“DbContext.Projects.Where(p =&gt; p.ProjectID&gt; 0)上获得了设计时编译错误”将AnonymousType#1转换为IEnumerable“。选择“
public List<Project> GetProjects()
{
try
{
using (YeagerTechEntities DbContext = new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
IEnumerable<Project> project = DbContext.Projects.Where(p => p.ProjectID > 0).Select(s => new
{
s.CategoryID,
s.Quote,
s.Name,
priname = s.Priority.Description,
catname = s.Category.Description,
statname = s.Status.Description,
s.Customer.Email,
s.Customer.City
});
List<Project> myProjects = new List<Project>();
myProjects = project.ToList();
return myProjects;
}
}
我无法使用第三个查询,因为我在“proj.ToList()”上获得了与上面相同的设计时编译错误。
public List<Project> GetProjects()
{
try
{
using (YeagerTechEntities DbContext = new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
IEnumerable<Project> project = DbContext.Projects.Where(p => p.ProjectID > 0);
var proj = project.Select(s => new
{
s.CategoryID,
s.Quote,
s.Name,
priname = s.Priority.Description,
catname = s.Category.Description,
statname = s.Status.Description,
s.Customer.Email,
s.Customer.City
}
);
List<Project> myProjects = new List<Project>();
myProjects = proj.ToList();
return myProjects;
}
}
catch (Exception ex)
{
throw ex;
}
}
有人可以告诉我如何从中间层对象正确传递LINQ查询的内容吗?回到客户端我知道如何使用带有“var”语法的查询从后面的代码或Controller中显然做到这一点。
基于jalpesh's answer below我添加了一个名为 ProjectFields 的类(在同一个项目中),并将该类合并到LINQ查询中。但是,我得到一个设计时编译错误“无效的匿名类型成员声明。必须使用成员分配,简单名称或成员访问声明匿名类型成员。”
设计时编译错误位于 projflds 属性的每一行。
我知道你们要我做什么。基本上,创建一个新类并在层之间发送该类。 我在这里做错了什么????
以下是我创建的新课程:
public class ProjectFields
{
public short CategoryID { get; set; }
public decimal Quote { get; set; }
public string Name { get; set; }
public string PriorityName { get; set; }
public string CategoryName { get; set; }
public string StatusName { get; set; }
public string Email { get; set; }
public string City { get; set; }
}
下面是修改后的LINQ查询,我收到错误:
public List<Project> GetProjects()
{
try
{
using (YeagerTechEntities DbContext = new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
IEnumerable<Project> project = DbContext.Projects.Where(p => p.ProjectID > 0);
ProjectFields projflds = new ProjectFields();
var proj = project.Select(s => new
{
projflds.CategoryID = s.CategoryID,
projflds.Quote = s.Quote,
projflds.Name = s.Name,
projflds.PriorityName = priname = s.Priority.Description,
projflds.CategoryName = catname = s.Category.Description,
projflds.StatusName = statname = s.Status.Description,
projflds.Email = s.Customer.Email,
projflds.City = s.Customer.City
});
List<Project> myProjects = new List<Project>();
myProjects = project.ToList();
return myProjects;
}
}
catch (Exception ex)
{
throw ex;
}
}
答案 0 :(得分:2)
您正在使用选择新属性创建AnonymousType,这就是该属性与项目类不同的原因。下面将创建一个新类型,所以首先你需要列出到列表,你不能直接投射它。
var proj = project.Select(s => new
{
s.CategoryID,
s.Quote,
s.Name,
priname = s.Priority.Description,
catname = s.Category.Description,
statname = s.Status.Description,
s.Customer.Email,
s.Customer.City
}
做一些像下面的链接。
Convert Anonymous Type to Class
var proj = project.Select(s => new Project
{
Name=s.Name
}
答案 1 :(得分:2)
你几乎只有几个最终解决方案:
public List<ProjectFields> GetProjects()
{
try
{
using (YeagerTechEntities DbContext = new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
IQueryable<Project> project = DbContext.Projects.Where(p => p.ProjectID > 0);
var proj = project.Select(s => new ProjectFields
{
CategoryID = s.CategoryID,
Quote = s.Quote,
Name = s.Name,
PriorityName = s.Priority.Description,
CategoryName = s.Category.Description,
StatusName = s.Status.Description,
Email = s.Customer.Email,
City = s.Customer.City
});
List<ProjectFields> myProjects = project.ToList();
return myProjects;
}
}
catch (Exception ex)
{
throw ex;
}
}