English 中文(简体)
更新数据
原标题:Update one DataTable from another
create table employee
(
    id int,
    name varchar(10),
    dept_id int,
    dept_name varchar(10)
);

insert into employee values(1,  ABC1 , 1,   );
insert into employee values(2,  ABC2 , 2,   );
insert into employee values(3,  ABC3 , 1,   );
insert into employee values(4,  ABC4 , 2,   );
insert into employee values(5,  ABC5 , 1,   );

create table dept
(
    dept_id int,
    dept_name varchar(10)
);

insert into dept values(1,  XYZ1 );
insert into dept values(2,  XYZ2 );
UPDATE e
SET    e.dept_name = d.dept_name
FROM   employee AS e
       JOIN dept AS d
       ON e.dept_id = d.dept_id

我如何将上述询问(用黑体字)转换为LINQ的询问?

问题回答

我不相信你。 Linq to is mean as a means to receive data in/out of the database for use in their application Code. 你们想要的是低级数据操纵,正如你的例子所示,这是在T-SQL中最好的操作。

你可以把两个表格的记录输入另一个物体,然后公布结果。

// I m doing this this way because I can t remember the syntax for 
// LoadOptions and want to be sure to avoid the SELECT N+1 issue.
var query = from e in db.Employees
            select new EmployeeDepartment
                       {
                           Employee = e,
                           Department = e.Department
                       };

foreach (var item in query)
{
    item.Employee.DepartmentName = item.Department.DepartmentName;
}

db.SubmitChanges();

However
You probably already know this, but it might be better to normalise your database, so that the department name is only on the department table, and use your employee->department relationship to get the department name for a given employee.

http://www.ohchr.org。

var employeeDept = from e in db.employees 
                  Join d in db.Depts on e.dept_id equals d.dept_id 
                  select new 
                  {Employee = e ,
                   Department = d};

foreach(var ed in employeeDept)
{
   ed.Employee.DepartmentName = ed.Department.DepartmentName;
}
db.submitChanges();

www.un.org/Depts/DGACM/index_spanish.htm 解决办法之一是:

var enumEmp = employee.AsEnumerable(); 
var enumDept = dept.AsEnumerable();

var employeeDept = from empl in enumEmp 
join d in enumDept on empl.Field<int>("dept_id") equals d.Field<int>("dept_id")
select new {enumEmp = empl , enumDept = d};

foreach(var ed in employeeDept)
{
ed.enumEmp.SetField<string>("dept_name",ed.enumDept.Field<string>("dept_name"));
}




相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签