简体   繁体   English

在C#中使用Entity Framework和lambda表达式的Gernerate List

[英]Gernerate List using Entity Framework and lambda expressions in C#

For my own practice I'm building a small ASP.NET website that shows data content of an existing database. 对于我自己的实践,我正在构建一个小型ASP.NET网站,该网站显示现有数据库的数据内容。

To work with the database I'm using Entity Framework and I want to use lambda expressions in connection. 为了使用数据库,我正在使用Entity Framework,并且我想在连接中使用lambda表达式。

Actual I got 6 tables in my database: 实际上我的数据库中有6个表:

  1. Projects (ID, ProjectName) Projects (ID,ProjectName)
  2. Categories (ID, CategoryName) Categories (ID,CategoryName)
  3. Users (ID, UserName) Users (ID,用户名)
  4. Parts (ID, PartName) Parts (ID,零件名)
  5. ProjectCategoryPart (ProjectID, CategoryID, PartID, PartExists) ProjectCategoryPart (ProjectID,CategoryID,PartID,PartExists)
  6. ProjectCategoryUser (ProjectID, CategoryID, UserID) ProjectCategoryUser (ProjectID,CategoryID,UserID)

    • Each Project has n Categories 每个Project都有n Categories
    • Each Category has 1 User 每个Category1 User
    • For each Project and Category there are n Parts with an PartExists -Flag 对于每个ProjectCategory ,都有n带有PartExists Parts

Now I want to create a list that contains the following information for one of the projects inside of table one 现在,我想为表一中的一个项目创建一个包含以下信息的列表

Category       User                Parts Left       Total Parts
Category_1     RelatedUserName     PartCountLeft    PartCountTotal
Category_2     RelatedUserName     PartCountLeft    PartCountTotal
Category_n     RelatedUserName     PartCountLeft    PartCountTotal

Here for example one code Line I'm actually using to get the data of a project out of the table Projects: 例如,在这里,我实际上正在使用的一个代码行从表Projects中获取项目的数据:

myList = DatabaseEntities.Projects.Single(project => project.Number == sendNumber);

Can someone help me with the code I need to create the List? 有人可以为我提供创建列表所需的代码吗?

Up to now I'm only able to create simple database requests because it was hard for me to find examples that are showing more than this. 到目前为止,我只能创建简单的数据库请求,因为很难找到显示更多信息的示例。

As explained in the comments, your design may need some work, but if you are trying to focus in on the aggregation portion you can work around it. 如评论中所述,您的设计可能需要做一些工作,但是如果您试图专注于聚合部分,则可以解决它。 If your classes have navigation properties similar to this: 如果您的班级具有类似于以下内容的导航属性:

public class Project
{
    public int Id { get; set; }  
    public string ProjectName { get; set; }

    // if you want multiple users you will need to change this
    public int UserId{ get; set; }
    public User User { get; set; }  // nav to user

    public ICollection<ProjectPart> ProjectParts { get; set; }
}

public class ProjectPart
{
    public int PartId { get; set; } // Be consistent and call this ID as below
    public string PartName { get; set; }
    public bool? PartExists { get; set; }
    public string CategoryName { get; set; }

    public int ProjectId { get; set; }
    public Project Project { get; set; }  // nav to project
}

public class User
{
    public int Id { get; set; }
    public string UserName{ get; set; }

    public ICollection<Project> Projects { get; set; }
}

Next, I would build a viewmodel for the results: 接下来,我将为结果构建一个视图模型:

public class ProjectPartViewModel
{
    public string Category { get; set; }
    public string Name { get; set; }
    public int PartsCount { get; set; }
    public int PartsExistCount { get; set; }
}

Then the query: 然后查询:

var projectPartCounts = context.ProjectParts.AsNoTracking()
    .GroupBy(pp => new { pp.CategoryName, pp.Project.User.UserName })
    .Select(g => new PropjectPartsViewModel {
        Category = g.Key.CategoryName,
        Name = g.Key.UserName,
        PartsCount = g.Count(),
        PartsExistCount = g.Count(pp => pp.PartsExist == true)
    })
    .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM