Click here to Skip to main content
15,914,162 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a code:

C#
DataTable empShifts = new DataTable("EmpShifts");
empShifts.Columns.Add("EmpId",typeof(string),null);
empShifts.Columns.Add("Month",typeof(string),null);
 
for(int i=1; i<= 31; i++)
    for(int j=1; j <=3; j++)
        empShifts.Columns.Add(i.ToString()+"-" + j.ToString(),typeof(string),null);
 
shiftData.AsEnumerable().GroupBy (dr => dr.Field<string>("EmpId")).Select (er => {
er.GroupBy (dr2 => dr2.Field<string>("Month")).Select (mr => {
    DataRow row = empShifts.NewRow();
    row["EmpId"]=er.Key;
    row["Month"]=mr.Key;
    mr.Select (ms => 
    	row[ms.Field<datetime>("Date").Day + "-" + ms.Field<int>("Shift").ToString()]="x").Count ();
    empShifts.Rows.Add(row);
    return mr;
    }).Count ();
return er;
}).Count ();

I want add columns "EmpName" and I did:

DataTable empShifts = new DataTable("EmpShifts");
empShifts.Columns.Add("EmpId",typeof(string),null);
empShifts.Columns.Add("Month",typeof(string),null);
empShifts.Columns.Add("EmpName",typeof(string),null);

 
for(int i=1; i<= 31; i++)
    for(int j=1; j <=3; j++)
        empShifts.Columns.Add(i.ToString()+"-" + j.ToString(),typeof(string),null);

//and:
 
shiftData.AsEnumerable().GroupBy (dr => dr.Field<string>("EmpId")).Select (er => {
er.GroupBy (dr2 => dr2.Field<string>("Month")).Select (mr => {
    DataRow row = empShifts.NewRow();
    row["EmpId"]=er.Key;
    row["Month"]=mr.Key;
    row["EmpName"]=??????
    mr.Select (ms => 
    	row[ms.Field<datetime>("Date").Day + "-" + ms.Field<int>("Shift").ToString()]="x").Count ();
    empShifts.Rows.Add(row);
    return mr;
    }).Count ();
return er;
}).Count ();


I don't know LinQ please help me
Posted
Updated 5-Jun-12 8:43am
v2

OK, I think you have some code outside the block you posted that will be of help. However, I think I can point you in the right direction. You are doing a Linq query on the variable named shiftData. That is where your data is coming from. Inside there should be your employee's name. You should be able to do the following:

C#
row["EmpName"]=er.EmpName;


That is a wild guess but I think that will work. You will need to change EmpName to be whatever the actual field name is.

The bottom line, however, is that if you don't know Linq really well, you shouldn't be doing this. Do this in ADO.NET using a T-SQL command and save yourself the headache. I know it isn't the cool way of doing things but knowing how your code works beats having cool code every time.
 
Share this answer
 
Comments
Manas Bhardwaj 14-Jun-12 16:19pm    
Good answer +5!
VJ Reddy 14-Jun-12 19:52pm    
Thank you, Manas :)
If the table ShiftData has a column EmpName then
replace row["EmpName"]=?????? with
C#
row["EmpName"]=er.First().Field<string>("EmpName");

as er is of IEnumerable which has multiple rows. Since the er is obtained by grouping on EmpId, each row has same EmpName. So the First row can be used to retrieve the EmpName.

But, instead of repeating the EmpName in every row of ShiftData corresponding to the EmpId, it is better to use a DataTable for empNames like

C#
DataTable empNames = new DataTable("EmpNames");
empNames.Columns.Add("EmpId",typeof(string),null);
empNames.Columns.Add("EmpName",typeof(string),null);

then row["EmpName"]=?????? can be replaced by
XML
DataRow empName = empNames.AsEnumerable().FirstOrDefault (
    n => n.Field<string>("EmpId")==er.Key);
row["EmpName"]= empName == null ? 
    string.Empty : empName.Field<string>("EmpName");

The above will use Empty string if the required EmpId is not available in the empNames DataTable
 
Share this answer
 
v3
Comments
Manas Bhardwaj 14-Jun-12 16:19pm    
Good answer +5!
I am not sure that this is the answer, but I think it should work:

replace

row["EmpName"]=??????


with

row["EmpName"]=mr.First(i => i.Field<string>("EmpId"))</string>


All mr and dr records should have the same EmpName if only a single EmpName is associated with each EmpId. You should be able to take any record in either collection and get the right name. Without being able to run it I cannot be sure.
 
Share this answer
 
v2
Comments
Manas Bhardwaj 14-Jun-12 16:20pm    
5ed!

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