【问题标题】:Linq Query For Many to Many Relation within Intersecting Table [duplicate]Linq查询相交表中的多对多关系[重复]
【发布时间】:2020-10-31 02:58:41
【问题描述】:

我有 2 个模型和一个相交模型。

我想使用 Linq Lambda 表达式为特定 Ticket Id 提取 Ticket 和共同响应 Users /p>

模型 1:票

public class Ticket
    {
        public int Id { get; set; } 
        public string Title { get; set; }

        public ICollection<UserTicket> UserTickets { get; set; }
    }

模型 2:用户

public class User : IdentityUser
    {
        // Id: string, UserName: string Automatic generated by ASP.NET Core Identity

        public string Gender { get; set; }
        public string Phone { get; set; }

        public ICollection<UserTicket> UserTickets { get; set; }
    }

相交模型:UserTicket

public class UserTicket
    {
        public string UserId { get; set; }
        public User User { get; set; }
        public int TicketId { get; set; }
        public Ticket Ticket { get; set; }
    }

数据库上下文:

public class DataContext : IdentityDbContext<User>
    {
        public DataContext(DbContextOptions<DataContext> options) : base(options) {}

        // public DbSet<User> Users { get; set; } ****Using IdentityDbContext

        public DbSet<Ticket> Tickets { get; set; }
        public DbSet<UserTicket> UserTickets { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
           
            builder.Entity<UserTicket>().HasKey(ut => new { ut.UserId, ut.TicketId });
            builder.Entity<UserTicket>()
                .HasOne(t => t.Ticket)
                .WithMany(t => t.UserTickets)
                .HasForeignKey(t => t.TicketId);
            builder.Entity<UserTicket>()
                .HasOne(t => t.User)
                .WithMany(t => t.UserTickets)
                .HasForeignKey(t => t.UserId);
         }
    }

我在这个领域非常初学者,尝试了很多,但无法解决这个问题。 请帮帮我。

【问题讨论】:

    标签: c# asp.net linq asp.net-core entity-framework-core


    【解决方案1】:

    你可以使用:

    dbcontext.Tickets.where(t=>t.Id==TICKET_ID)
      .include(t=>t.UserTickets)
      .ThenInclude(ut=>ut.User);
    

    查看类似问题:How to query many-to-many releationship in EF Core

    最好看看entity-framework-core Tutorials 学习Entity Framework Core的基础知识;

    【讨论】:

      【解决方案2】:

      您可以执行以下 LINQ 查询来获取特定 ticketId 的所有用户的列表:

      await DataContext.UserTicket
          .Include(t => t.User)
          .Where(t => t.TicketId == ticketId)
          .Select(t => t.User)
          .ToListAsync()
      

      【讨论】:

        【解决方案3】:

        可能是这样的:

        public class TicketViewModel
        {
           //Your ticket properties
           public IEnumerable<UserViewModel> Users {get; set;}
        }
        
        public class UserViewModel
        {
            //Your user properties        
        }
        
        //query
        
        var ticket = DataContext.Tickets
          .Include(t => t.UserTickets)
            .ThenInclude(ut => ut.User)
          .Where(t => t.Id == {ID})
          //Here initialize required properties
          .Select(t => new TicketViewModel {Users = t.UserTickets.Select(ut => new UserViewModel {Id = ut.User.Id /* etc */})})
          .FirstOrDefault()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-06-22
          • 2019-11-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-04
          • 2021-01-30
          相关资源
          最近更新 更多