Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 如何设置不同表列的值?_Linq_Lambda - Fatal编程技术网

Linq 如何设置不同表列的值?

Linq 如何设置不同表列的值?,linq,lambda,Linq,Lambda,我有两张桌子 第一个表的架构: UserId:11,ReportId=1400 UserId:25,ReportId=1510 UserId:30,ReportId=1518 UserId:41,ReportId=1550 UserId:50,ReportId=1590 第二个表的架构: UserId:50,ReportId=1400,DeptId=10 UserId:80,ReportId=1510,DeptId=10 UserId:20,ReportId=1518,DeptId=10

我有两张桌子

第一个表的架构:

  • UserId:11,ReportId=1400
  • UserId:25,ReportId=1510
  • UserId:30,ReportId=1518
  • UserId:41,ReportId=1550
  • UserId:50,ReportId=1590
第二个表的架构:

  • UserId:50,ReportId=1400,DeptId=10
  • UserId:80,ReportId=1510,DeptId=10
  • UserId:20,ReportId=1518,DeptId=10
  • UserId:41,ReportId=1550,DeptId=10
  • UserId:50,ReportId=1590,DeptId=10
  • UserId:60,ReportId=1525,DeptId=10
  • UserId:90,ReportId=1526,DeptId=10
  • UserId:50,ReportId=1410,DeptId=10
  • UserId:50,ReportId=1489,DeptId=10
  • UserId:50,ReportId=1327,DeptId=10
我需要将第一个表的UserId更改为第二个表的UserId,其中ReportId相同

我的意思是:

  • 用户ID:50,报告ID=1400
  • 用户ID:80,报告ID=1510
  • 用户ID:20,报告ID=1518
  • 用户ID:41,报告ID=1550
  • 用户ID:50,报告ID=1590
List tableFirst=新列表();
tableFirst=FirstTableBL.GetAll();
列表表秒=新列表();
tableSecond=SecondTableBL.GetAll();
对于(int i=0;ik.ReportId==tableSecond[i].ReportId).UserId;
}
但是我得到了一个空定义错误,因为tableSecond没有tableFirst的所有值


对于这种情况,正确的linq表达式是什么?

FirstOrDefault
如果没有找到任何内容,将返回null。您应该在代码中进行空检查,然后继续:

for (int i = 0; i < tableSecond.Count(); i++)
{
  var fromTableFirst = tableFirst.FirstOrDefault(k => k.ReportId == tableSecond[i].ReportId)

  if(fromTableFirst != null)
      tableSecond[i].UserId = fromTableFirst.UserId;
}
for(int i=0;ik.ReportId==tableSecond[i].ReportId)
if(fromTableFirst!=null)
tableSecond[i].UserId=fromTableFirst.UserId;
}

FirstOrDefault
如果找不到任何内容,将返回null。您应该在代码中进行空检查,然后继续:

for (int i = 0; i < tableSecond.Count(); i++)
{
  var fromTableFirst = tableFirst.FirstOrDefault(k => k.ReportId == tableSecond[i].ReportId)

  if(fromTableFirst != null)
      tableSecond[i].UserId = fromTableFirst.UserId;
}
for(int i=0;ik.ReportId==tableSecond[i].ReportId)
if(fromTableFirst!=null)
tableSecond[i].UserId=fromTableFirst.UserId;
}

FirstOrDefault
如果找不到任何内容,将返回null。您应该在代码中进行空检查,然后继续:

for (int i = 0; i < tableSecond.Count(); i++)
{
  var fromTableFirst = tableFirst.FirstOrDefault(k => k.ReportId == tableSecond[i].ReportId)

  if(fromTableFirst != null)
      tableSecond[i].UserId = fromTableFirst.UserId;
}
for(int i=0;ik.ReportId==tableSecond[i].ReportId)
if(fromTableFirst!=null)
tableSecond[i].UserId=fromTableFirst.UserId;
}

FirstOrDefault
如果找不到任何内容,将返回null。您应该在代码中进行空检查,然后继续:

for (int i = 0; i < tableSecond.Count(); i++)
{
  var fromTableFirst = tableFirst.FirstOrDefault(k => k.ReportId == tableSecond[i].ReportId)

  if(fromTableFirst != null)
      tableSecond[i].UserId = fromTableFirst.UserId;
}
for(int i=0;ik.ReportId==tableSecond[i].ReportId)
if(fromTableFirst!=null)
tableSecond[i].UserId=fromTableFirst.UserId;
}

单向,使用
DefaultIfEmpty

tableSecond[i].UserId= tableFirst
    .Where(k => k.ReportId== tableSecond[i].ReportId)
    .Select(k => k.UserId)
    .DefaultIfEmpty(new Nullable<int>())  // replace as desired
    .First();
tableSecond[i].UserId=tableFirst
.Where(k=>k.ReportId==tableSecond[i].ReportId)
.Select(k=>k.UserId)
.DefaultIfEmpty(新的Nullable())//根据需要替换
.First();

单向,使用
DefaultIfEmpty

tableSecond[i].UserId= tableFirst
    .Where(k => k.ReportId== tableSecond[i].ReportId)
    .Select(k => k.UserId)
    .DefaultIfEmpty(new Nullable<int>())  // replace as desired
    .First();
tableSecond[i].UserId=tableFirst
.Where(k=>k.ReportId==tableSecond[i].ReportId)
.Select(k=>k.UserId)
.DefaultIfEmpty(新的Nullable())//根据需要替换
.First();

单向,使用
DefaultIfEmpty

tableSecond[i].UserId= tableFirst
    .Where(k => k.ReportId== tableSecond[i].ReportId)
    .Select(k => k.UserId)
    .DefaultIfEmpty(new Nullable<int>())  // replace as desired
    .First();
tableSecond[i].UserId=tableFirst
.Where(k=>k.ReportId==tableSecond[i].ReportId)
.Select(k=>k.UserId)
.DefaultIfEmpty(新的Nullable())//根据需要替换
.First();

单向,使用
DefaultIfEmpty

tableSecond[i].UserId= tableFirst
    .Where(k => k.ReportId== tableSecond[i].ReportId)
    .Select(k => k.UserId)
    .DefaultIfEmpty(new Nullable<int>())  // replace as desired
    .First();
tableSecond[i].UserId=tableFirst
.Where(k=>k.ReportId==tableSecond[i].ReportId)
.Select(k=>k.UserId)
.DefaultIfEmpty(新的Nullable())//根据需要替换
.First();

如果没有值,你想分配什么?如果没有值,你想分配什么?如果没有值,你想分配什么?如果没有值,你想分配什么?如果没有匹配,我认为OP不想更改
用户ID
。因此,“我需要将第一个表的UserId更改为第二个表的UserId,其中ReportId是相同的。”@IronMan84:然而,也许我们的答案可以完美地互补;)我认为OP不希望在没有匹配项的情况下更改
用户ID
。因此,“我需要将第一个表的UserId更改为第二个表的UserId,其中ReportId是相同的。”@IronMan84:然而,也许我们的答案可以完美地互补;)我认为OP不希望在没有匹配项的情况下更改
用户ID
。因此,“我需要将第一个表的UserId更改为第二个表的UserId,其中ReportId是相同的。”@IronMan84:然而,也许我们的答案可以完美地互补;)我认为OP不希望在没有匹配项的情况下更改
用户ID
。因此,“我需要将第一个表的UserId更改为第二个表的UserId,其中ReportId是相同的。”@IronMan84:然而,也许我们的答案可以完美地互补;)