如何在LinQ中添加列 [英] How do add colums in LinQ
问题描述
我有一个代码:
I have a code:
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 ();
我不知道LinQ请帮助我
I don''t know LinQ please help me
推荐答案
好吧,我认为您在发布的代码块之外还有一些代码会有所帮助.但是,我认为我可以为您指明正确的方向.您正在对名为shiftData
的变量进行Linq查询.那就是您的数据来源.里面应该有你的员工的名字.您应该能够执行以下操作:
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 namedshiftData
. That is where your data is coming from. Inside there should be your employee''s name. You should be able to do the following:
row["EmpName"]=er.EmpName;
这是一个疯狂的猜测,但我认为这会起作用.您需要将EmpName
更改为实际的字段名称.
但是,最重要的是,如果您不太了解Linq,则不应该这样做.使用T-SQL命令在ADO.NET中执行此操作,从而省去了您的麻烦.我知道这不是一种很酷的处理方式,但是每次都知道自己的代码工作原理,每次都会拥有很酷的代码.
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.
如果表ShiftData
有一列EmpName
然后
将row["EmpName"]=??????
替换为
If the tableShiftData
has a columnEmpName
then
replacerow["EmpName"]=??????
with
row["EmpName"]=er.First().Field<string>("EmpName");
因为er
是IEnumerable
的,所以具有多个行.由于er
是通过对EmpId
进行分组而获得的,因此每一行都具有相同的EmpName
.因此First
行可用于检索EmpName
.
但是,与其在与EmpId
相对应的ShiftData
的每一行中重复EmpName
,不如对empNames
like
使用DataTable
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);
然后row["EmpName"]=??????
可以替换为
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");
如果所需的EmpId
在empNames
DataTable
The above will use Empty
string if the required EmpId
is not available in the empNames
DataTable
中不可用,则以上将使用Empty
string :
替换
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>
如果每个EmpId
仅关联一个EmpName
,则所有mr
和dr
记录都应具有相同的EmpName
.您应该能够在任一集合中取得任何记录并获得正确的名称.无法运行它,我不确定.
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.
这篇关于如何在LinQ中添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!