簡體   English   中英

如何從第二個表中獲取多個記錄基於第一個表中的記錄列表使用EF

[英]How to get multiple Records from Second table base on list of record from First table Using EF

我在Sql數據庫中有兩個表,比如Users和UserEducation表。 用戶表中有多個記錄,每個用戶在UserEducation表中有一條記錄。 我想通過加入這兩個表來顯示網格視圖中的所有記錄? 我如何使用Entity Framework執行此操作?

您只需使用User中的navigationproperty即可:

var user = new User();
user.UserEducation.[Property];

因為有一對一的映射,我寧願制作一個表。 但具體來說你可以這樣:

entityframeworkContext obj = new entityframeworkContext(); 列出xyz = obj.database.SqlQuery(“選擇u.fieldname1作為modeltablefield1,u.fieldname2作為modeltablefield2,ued.fieldname1作為modeltablefield3,ued.fieldname2作為modeltablefield4來自用戶u內部加入UserEducation ued on u.commonfield = ued.commonfield” );

這里常見的字段將是第二個表中的外鍵模型表是您從組合查詢所需的任何邏輯表(MVC特定)希望它適合您!

我創建了一個控制台應用程序來滿足您的用例。 您必須將此代碼重用到您希望將輸出綁定到網格視圖的Windows窗體或基於Web的應用程序中。 我假設POCO課程中有一些屬性,您可以隨時根據您為用戶和教育實體保存的所有值來修改它們。 我的代碼片段中沒有提到任何連接字符串。 如果您沒有提及連接字符串,實體框架會自動連接到計算機上的sql express數據庫,或者它將連接到您在app.config或web.config文件中提到連接字符串的數據庫。 希望這可以幫助!

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace UsersCodeFirst
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var db = new EfContext())
            {
                // Display all users with their education from the database 
                var query = from user in db.Users 
                           join userEducation in db.UserEducations
                           on user.UserId equals userEducation.UserId
                    orderby user.Name
                    select new
                    {
                        Name = user.Name,
                        UserEducation = userEducation.CourseName
                    };

                //bind to grid by setting grid data source to query.ToList()
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }

    public class User
    {
        public string UserId { get; set; }
        public string Name { get; set; }
        public virtual List<Education> Posts { get; set; }
    }

    public class Education
    {
        public string EducationId { get; set; }
        public string CourseName { get; set; }
        public string UserId { get; set; }
    }

    public class EfContext : DbContext
    {
        public DbSet<Education> UserEducations { get; set; }
        public DbSet<User> Users { get; set; }
    }
}

您需要將Entity Framework nuget包添加到項目中以獲取對DbContext類的引用。

或者使用Linq to Entity和連接表:

using ( var context = new YourContext)
   {
     var users = context.UserDbSet;
     var userEdications = context.UserEdication.DbSet ;

     var joinedTables =  from user in users
                         join userEdication in userEdications on user.userId  = userEdication.userId 
                         select new OutPutEntity
                         {
                               Name = user.Name,
                               Edication = userEdication.Edication
                         }

                         gridView.DataSource = joinedTables.toList(); // should be placed outside the using. (here just as a sample)
   }

優點 - 您可以在IQurable級別指定輸出格式。 下行 - 看起來很復雜。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM