Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to fill an aggregate class in C #?
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();
Posted
Updated 20-May-15 16:13pm
v5
Comments
Sergey Alexandrovich Kryukov 18-May-15 23:19pm    
What does it mean, "fill a class"? Fill where, with what?
—SA
Thyago Analista 19-May-15 21:13pm    
Basically I would like to fill a property which is of type list using LINQ possible?

If as in my example above I'm using an aggregate class.
StM0n 19-May-15 0:22am    
Could you please reformat your code?
Thyago Analista 20-May-15 12:05pm    
So have a method that returns a DataTable, I would fill this List <oderdetails> Class Order, ie, bringing a collection of OrderDetails. For example in my'm using SQL "SELECT O.Id, O.date, O.person, O.value, D. * FROM INNER JOIN Order ON OrderDetails O.id = D.fk_Order," how do I move the data to the OrderDetails collection Class Order?
Tomas Takac 19-May-15 2:47am    
1) You are selecting data rows not entities. The entities need to be created somehow. Use proper ORM! Why to load data into datasets and then hydrate your entities manually? BTW don't you see you are doing the same join twice here?

2) Your Employee class has collection of Clients which implies one employee to many clients relationship. Yet in database it's the employee table holding the FK to client which implies reverse, one client to many employees, relationship. Which one is it then?

3) Your linq query implies ids are guids but your Client entity declares Id as integer.

1 solution

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.

C#
// 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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900