asp.net MVC - 将表连接起来,并将名称命名为模型 MVC、LINQ、C#、模型

本文关键字:模型 MVC 命名为 LINQ net 连接 asp 起来 | 更新日期: 2023-09-27 17:57:13

这是我的Office模型

public class Office 
    {
        public int ID { get; set; }
        public string Color { get; set; }
    }

这是我的工作模型

public class Worker 
    {
        public int ID { get; set; }
        public int OfficeID { get; set; }
        public string FullName { get; set; }
        [NotMapped]
        public string OfficeColor { get; set; }
    }

我想要的是让所有使用 LINQ 与办公室颜色的工作人员获得指定 OfficeID 颜色的颜色

asp.net MVC - 将表连接起来,并将名称命名为模型 MVC、LINQ、C#、模型

在两个 EF 模型之间建立关系的正确方法(代码优先方法):

public class Worker 
{
    public int ID { get; set; }
    public int OfficeID { get; set; }
    public virtual Office Office { get; set; }
    public string FullName { get; set; }
    // [NotMapped]
    // public string OfficeColor { get; set; }
}
public class Office 
{
    private ICollection<Worker> workers;
    public Office 
    {
       this.workers = new HashSet<Worker>();
    } 
    public int ID { get; set; }
    public string Color { get; set; }
    public virtual ICollection<Worker> Workers
    {
        get { return this.workers; }
        set { this.workers = value; }
    }
}
// and get all workers from Db
public class Program
{
     public static void Main()
     {
       IEnumerable<WorkerViewModel> workers = DbContext.Workers.All().Select(w => 
          new WorkerViewModel()
          {
               Id = w.Id,
               OfficeId = w.Office.Id,
               FullName = w.FullName,
               OfficeColor = w.Office.Color
          })
          .ToList();
     }
}

尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string myColor = "Red";
            var results = (from office in Office.offices
                           join worker in Worker.workers on office.ID equals worker.ID
                           select new { office = office, worker = worker })
                            .Where(x => x.office.Color == myColor)
                            .Select(x => new {
                                ID = x.worker.ID,
                                OfficeID = x.worker.OfficeID, 
                                FullName = x.worker.FullName, 
                                OfficeColor = x.office.Color  
                            }).ToList();
        }
    }
    public class Office
    {
        public static List<Office> offices { get; set; }
        public int ID { get; set; }
        public string Color { get; set; }
        public Office()
        {
            office = new Office();
        }
    }
    public class Worker
    {
        public static List<Worker> workers { get; set; }
        public int ID { get; set; }
        public int OfficeID { get; set; }
        public string FullName { get; set; }
        //[NotMapped]
        public string OfficeColor { get; set; }
        public Worker()
        {
            workers = new List<Worker>();
        }
    }
}