如何使用Id将一个表的数据添加到另一个表中并使用.net API显示

enyaitl3  于 2023-06-25  发布在  .NET
关注(0)|答案(1)|浏览(105)

我有两个表在我的DB。一个是国家表,另一个是国家表。
Country表具有ID、名称、states[]。状态表具有Id、stateName、countryId。
我需要获取一个国家的详细信息的基础上使用ID。
每当我使用国家ID对国家使用get操作时-我应该以数组格式获得ID,名称和所有国家ID的详细信息。
例如,我正在获取ID = 1的国家,并且它有2个状态,那么,输出应该是,

Id: 1,
Name: abc,
States: [
{
Id:1,
stateName: st1,
countryId: 1
},
{
Id:2,
stateName: st2,
countryId:1
}]

我需要使用join吗?或者别的什么。我需要在DAL中编写什么代码才能得到这样的输出?救命啊!我是.Net的新手

vwkv1x7d

vwkv1x7d1#

你有两种方法来获取数据。1. EF和2.adoNet

1.EF

你可以用EF
首先你必须添加类(两个类),一个类是国家,另一个类是状态,因为国家有很多状态,你必须使用“ICollection”
课程

public class Country
    {
        public int Id { set; get; }
        public string name { set; get; }
        public virtual ICollection<states> states { set; get; }

    }
    public class states
    {
        public int Id { set; get; }
        public string stateName { set; get; }
        public int countryId { set; get; }
        public virtual Country country { set; get; }

    }

这是DbContext中的代码(必须添加类)

public DbSet<Country> Country { get; set; }
public DbSet<states> states { get; set; }

这是插入数据(国家/地区,状态)的代码,在savechchange后的Countryid

var Country = new Country();
            Country.name = "test2";
            Country.states = new Collection<states>();
           
            var state = new states();
            state.stateName = "st1";
            Country.states.Add(state);
            state = new states();
            state.stateName = "st2";
           
            Country.states.Add(state);
            context.Country.Add(Country);
            context.SaveChanges();

这是获取数据(国家,状态)的代码,它使用Include获取国家的状态

var d = context.Country.Include(d => d.states).FirstOrDefault(d=>d.Id==2);

Ado.net

DataBaseCommon _GetData = new DataBaseCommon();
var _list = _GetData.GetData(ConntionStringSource);
 var _result = _GetData.getCountry(_list);
public class DataBaseCommon
    {
        public DataTable GetData(string connectionString)
        {
            DataTable dtPerson = new DataTable();

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                con.Open();
                var _SqlCommand = "SELECT  b.*,a.stateName,a.countryId\r\n  FROM [TestDB7].[dbo].[Country] b\r\n  inner join [TestDB7].[dbo].[states] a on a.countryId=b.Id";
                SqlCommand objSqlCommand = new SqlCommand(_SqlCommand, con);
                SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter(objSqlCommand);
                try
                {
                    objSqlDataAdapter.Fill(dtPerson);

                }
                catch (Exception ex)
                {
                    con.Close();
                }
            }

            return dtPerson;

        }

        public  List<Country> getCountry( DataTable table)
        {

            var _listCountry =
               from p in table.AsEnumerable()
               group p by p.Field<int>("Id") into g
               select new Country
               {
                   Id = g.Key,
                   name = g.Select(i => i.Field<string>("Name")).FirstOrDefault(),
                   states = table.AsEnumerable()
                           .Where(row => g.Any(p => g.Key == row.Field<int>("CountryId")))
                           .Select(d => new states
                           {
                               stateName = d.Field<string>("stateName"),
                               countryId = d.Field<int>("countryId")

                           }).ToList()
               };



            return _listCountry.ToList();

        }

    }

相关问题