您好我有两个linq声明:
from d in dtDummy.AsEnumerable()
where d["Role_id"].ToString() == "0"
select d;`
from d in dtDummy.AsEnumerable()
join t in dtTest.AsEnumerable() on d.Field<int>("role_id")
equals t.Field<int>("role_id")
where (d["role_dept"].ToString() != t["role_dept"].ToString() || d["role_dept"].ToString() != t["role_dept"].ToString() || d["role_desc"].ToString() != t["role_desc"].ToString() || d["role_all_clients"].ToString() != t["role_all_clients"].ToString() || d["role_admin"].ToString() != t["role_admin"].ToString() || d["role_super_admin"].ToString() != t["role_super_admin"].ToString())
select d;
如何编写一个linq单一语句,以获得与我在两个语句中进行联合时获得的结果相同的结果。
答案 0 :(得分:4)
您可以使用Union
:
var union = (from d in dtDummy...).Union
(from d in dtDummy...);
我个人认为,为了清晰起见,我们将两者分开:
var query1 = ...;
var query2 = ...;
var union = query1.Union(query2);
我还使用Field<string>
而不是调用ToString
...或者可能将这些投影到更干净的对象模型中,这样可以使您的查询更加简单。
答案 1 :(得分:1)
(from d in dtDummy.AsEnumerable() where d["Role_id"].ToString() == "0" select d).union
(from d in dtDummy.AsEnumerable() join t in dtTest.AsEnumerable() on d.Field<int>("role_id") equals t.Field<int>("role_id") where (d["role_dept"].ToString() != t["role_dept"].ToString() || d["role_dept"].ToString() != t["role_dept"].ToString() || d["role_desc"].ToString() != t["role_desc"].ToString() || d["role_all_clients"].ToString() != t["role_all_clients"].ToString() || d["role_admin"].ToString() != t["role_admin"].ToString() || d["role_super_admin"].ToString() != t["role_super_admin"].ToString()) select d)