日期:2014-05-20  浏览次数:20960 次

linq联合查询后排除相同项.


如上2张表
现在想要的是查询的时候用
主表的字段 CreatorId,IsCompleted,Remarks,CreateDate和子表字段MovieId
做查询条件
查询后将主表字段中相同的排除,
SQL code
select DISTINCT VOD_VodOrder.* from VOD_VodOrder join 
VOD_VodOrderItem on VOD_VodOrder.Id = VOD_VodOrderItem.ParentId where .....

上面语句能实现,但是用linq怎么做

------解决方案--------------------
查询出来是什么是根据你SELECT后面的东西决定的。
比如你可以
(from t1 in VodOrders join t2 in VodItems on t1.Id equals t2.ParentId select t1).Distinct(); /Distinct(IEqualityComparer<T>);


------解决方案--------------------
linq也有distinct,也有group。
------解决方案--------------------
C# code

var dd=from t1 in VodOrders join t2 in VodItems on t1.Id equals t2.ParentId where...
select ... ;
dd = dd.Distinct();

------解决方案--------------------
给你一个参考吧
var varQnum = from pRow in dttRuleData.AsEnumerable()
where pRow.Field<string>("NODE_TYPE") == GetLinqQuery(FlowRule.C_TYPE_N)
group pRow by pRow.Field<string>("NODE_VALUE")
into g
select new
{
g.Key,
nodeCode = g.Count()
};
------解决方案--------------------
var result = r1 from vod_vodorder
join r2 from vod_vodorderitem on r1.id eauals r2.id
where r1.fileld<string >("creatorid") = 变量 &&
r1.fileld<int>("iscompleted") = 变量 && 
r1.fileld<string>("remarks") = 变量 && 
r2.fileld<int>("moveid") = 变量 
select new 
{
id = r1.files<string>("id"),
creatorid = r1.fileld<int>("creatorid") ,
iscompleted =r1.fileld<int>("iscompleted"),
remarks = r1.fileld<int>("remarks"),
createdate = r1.fileld<datetime>("createdate")
}.distince().tolist();