If the table
ShiftData
has a column
EmpName
then
replace
row["EmpName"]=??????
with
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
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
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