【问题标题】:How to assign values to a list by linq without for or foreach statements?如何在没有 for 或 foreach 语句的情况下通过 linq 将值分配给列表?
【发布时间】:2016-06-18 07:25:53
【问题描述】:

我想比较两个列表并将第一个列表分配给另一个列表以备不时之需。

var getdetail=_readonlyservice.getdetail().ToList();
foreach(var item in docdetail)
{
      var temp=getdetail.firstordefualt(i=>i.Id=item.Id)
      if(temp==null) continue;
      item.code=temp.code;
}

我想在 linq 中实现 top 语句。有什么帮助吗?

【问题讨论】:

标签: c# linq


【解决方案1】:

这样想..

var getdetail=_readonlyservice.getdetail().ToList();
var tempList = from dd in context.docdetail
                join g in context.getdetail on dd.Id equals g.Id
               select new // Your type
               {
                 // Columns...
                 Code = g.Code
               }

【讨论】:

  • 如何通过 where 或 foreach 语句来实现你的代码?
【解决方案2】:

我相信你正在尝试像我一样做,虽然我要加入表。

var result = (from e in DSE.employees
             join d in DSE.departments on e.department_id equals d.department_id
             join ws in DSE.workingshifts on e.shift_id equals ws.shift_id
                              select new
                              {
                                  FirstName = e.FirstName,
                                  LastName = e.LastName,
                                  Gender = e.Gender,
                                  Salary = e.Salary,
                                  Department_id = e.department_id,
                                  Department_Name = d.department_name,
                                  Shift_id = ws.shift_id,
                                  Duration = ws.duration,
                              }).ToList();
                // TODO utilize the above result

我正在使用 DTO 方法来执行此操作。然后你返回结果(因为这种情况是结果)。 您可以查看整个问题和解决方案here

在这种情况下,您不需要放置 foreach 循环,正如 yourdatabase.table

每一行 中的查询所说

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 2010-10-07
    相关资源
    最近更新 更多