使用Linq C#从dataTable填充聚合类 [英] Fill aggregate class from dataTable using Linq C #

查看:94
本文介绍了使用Linq C#从dataTable填充聚合类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中填充聚合类?

  //   
public class 员工
{
public string 名称{ get ; set ; }
public string 部门{ get ; set ; }
public double 薪资{ get ; set ; }
public 列表< client> client = new < list>< client>();

公共员工(客户objClient)
{
.client.Add(objClient);
}
}

public class 客户
{
public int ID {获得; set ; }
public string 名称{ get ; set ; }
public string 国家{获取; set ; }
public string 地址{ get ; set ; }
}

// 在表单中选择返回DataTable
var ds = cnn.ExecutarSelectDataSet( 选择e.Name,e.Department,e.Salary,e.fk_client,c.Id,c.Name,c.Country,c .Anddess +
来自员工e +
INNER JOIN客户端c +
ON e.fk_client = c.id);

// 我无法填写List< client>物业客户类职员
列表< employee> linqquery =( from dr in ds.Tables [ 员工]。AsEnumerable()
join dr2 in ds.Tables [ Client]。AsEnumerable()$ dr.Field上的b $ b< guid>( fk_client)等于dr2.Field< guid> ( id
select dr)。ToList();

解决方案

我是Tomas Takac所说的。我不明白在sql查询中连接一次然后再在linq中连接的必要性。为什么不直接从sql获取连接数据?





但是如果你正在查看将数据表转换为List< employee>的代码,然后创建一个新的员工类对象,并从数据行设置属性值。



例如



  //  在您的linq查询中,将select dr替换为类似下面提到的代码 
选择 new 员工
{
姓名=博士.Field< string>( name),
Department = dr.Field< string> ;( department
...
};
< / string > < / string >


How to fill an aggregate class in C #?

//Class
public class Employee
{
    public string Name { get; set; }
    public string Department { get; set; }
    public double Salary { get; set; }
    public List<client> client = new <list><client>();

    Public Employee(Client objClient)
    {
        this.client.Add(objClient);
    }
}

public class Client
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public string Address { get; set; }
}

//In form a Select returning a DataTable
 var ds= cnn.ExecutarSelectDataSet("select e.Name, e.Department, e.Salary,e.fk_client,c.Id, c.Name,c.Country,c.Anddess" +
                                  "from Employee e" +
                                  "INNER JOIN Client c" +
                                  "ON e.fk_client = c.id");

//I am unable to fill the List <client> Property client Class Employees
List<employee> linqquery = (from dr in ds.Tables["Employee"].AsEnumerable()
    join dr2 in ds.Tables["Client"].AsEnumerable()
    on dr.Field<guid>("fk_client") equals dr2.Field<guid>("id")
     select dr).ToList();

解决方案

I second what Tomas Takac is saying. I do not understand the necessity to do join once in the sql query and then again in linq. Why not get the joined data directly from sql?


But if you are looking at code to convert datatable to List<employee>, then create a new object of employee class and set the property values from the datarow.

E.g.

// In your linq query replace the select dr to something like the below mentioned code
select new Employee
{
 Name = dr.Field<string>("name"),
 Department = dr.Field<string>("department")
 ...
}; 
</string></string>


这篇关于使用Linq C#从dataTable填充聚合类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆