-1

I want to compare two lists and assign 1st list to another in case of requirement.

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;
}

I want to implement top statements in linq .any help ?

Kahbazi
  • 14,331
  • 3
  • 45
  • 76
Mohsen Zahedi
  • 651
  • 2
  • 10
  • 28

2 Answers2

1

Think so..

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
               }
neer
  • 4,031
  • 6
  • 20
  • 34
1

I believe you are trying to do like the way I did, although I was going to join table.

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

I was using DTO method to do this. And then you return result(as this case is result). You may view the whole question and solution here.

As this case, you are not required to put foreach loop, as the query said from every row in yourdatabase.table

Lawraoke
  • 540
  • 5
  • 25