0

在破译从驻留在中间层类中的 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);

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

                    myProjects = project.ToList();

                    return myProjects;

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

我无法使用第二个查询,因为在“DbContext.Projects.Where(p => p.ProjectID > 0).Select”上出现设计时编译错误“converting the AnonymousType#1 to 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”语法的查询来完成它。


修订 #1

根据下面 jalpesh 的回答,我添加了一个名为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;
    }
}
4

2 回答 2

2

您正在创建一个带有选择新属性的 AnonymousType,这就是该属性与项目类不同的原因。下面将创建一个新类型,所以首先你需要列出你不能直接转换的 List。

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
                }

做一些类似下面链接的事情。

将匿名类型转换为类

var proj = project.Select(s => new Project
            {
                Name=s.Name
            }
于 2013-07-12T14:41:16.373 回答
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;
    }
}
于 2013-07-15T19:35:11.203 回答