0
我想將我的SQL查詢轉換爲LINQ實體。LINQ實體返回重複輸出
這是我的SQL查詢
SELECT DISTINCT
voidSEVT.RESTYPE, voidSEVT.SESID, voidWO.WONUM, voidSEVT.T_START,
voidSEVT.USER3
FROM
VOIDWO AS voidWO
JOIN
voidSEVT ON voidWO.SEQNUM = voidSEVT.SEQNUM
LEFT OUTER JOIN
PROJECTS AS prj ON voidWO.PRJ_ID = prj.PRJ_ID
LEFT OUTER JOIN
RESTYPE ON voidSEVT.RTYPE = RESTYPE.CODE
JOIN
RSRCE as resource on voidSEVT.RESID = resource.RESID
WHERE
(voidSEVT.USERFLAG4 != 1)
AND (LRTRIM(RTRIM(voidSEVT.USER3)) <> '')
我的SQL查詢輸出
RESTYPE SESID WONUM T_START USER3
0 7245100 1477491 1352239200 0030\r\n0060\r\n
4 7245098 1477491 1352239200 0030\r\n0060\r\n
4 7245099 1477491 1352239200 0030\r\n0060\r\n
5 7245097 1477491 1352239200 0030\r\n0060\r\n
5 7245101 1477491 1352239200 0030\r\n0060\r\n
這是我的LINQ到實體像下面
public List<ReversalEntity> ReversalList()
{
var db = new SchedwinEntities();
var query =
(from voidWO in db.VOIDWOes
join voidSEVT in db.VOIDSEVTs on voidWO.SEQNUM equals voidSEVT.SEQNUM into voidSEVT_join
from voidSEVT in voidSEVT_join.DefaultIfEmpty()
join prj in db.PROJECTS on voidWO.PRJ_ID equals prj.PRJ_ID into prj_join
from prj in prj_join.DefaultIfEmpty()
from voidSevtJoin in voidSEVT_join.DefaultIfEmpty()
join restype in db.RESTYPEs on voidSevtJoin.RTYPE equals restype.CODE into RESTYPEs_join
from restypes in RESTYPEs_join.DefaultIfEmpty()
join resource in db.RSRCEs on voidSEVT.RESID equals resource.RESID into RSRCS_join
where
(voidSEVT.USERFLAG4.value != 1) &&
(voidSEVT.USER3.Trim().Length > 0)
select new
{
resourceType = voidSEVT.RESTYPE,
voidSEVT.SESID,
wonum = voidWO.WONUM,
start = voidSEVT.T_START,
usage = voidSEVT.USER3,
SAPCCr = ((voidSEVT.RESTYPE == 5 || voidSEVT.RESTYPE == 0)?prj.USER3:prj.USER9),
activity = restypes.USER2,
sendercc = restypes.USER1,
resdesc = RSRCS_join.FirstOrDefault().DES,
Type=voidSEVT.RESTYPE,
jobdesc = voidWO.JOBDESC,
}).Distinct();
我的SQL查詢返回20個recrords
RESTYPE SESID WONUM T_START USER3
0 7245100 1477491 1352239200 0030\r\n0060\r\n
0 7245100 1477491 1352239200 0030\r\n0060\r\n
0 7245100 1477491 1352239200 0030\r\n0060\r\n
0 7245100 1477491 1352239200 0030\r\n0060\r\n
4 7245098 1477491 1352239200 0030\r\n0060\r\n
4 7245098 1477491 1352239200 0030\r\n0060\r\n
4 7245098 1477491 1352239200 0030\r\n0060\r\n
4 7245098 1477491 1352239200 0030\r\n0060\r\n
etc...
基本上每個記錄對於每條記錄都有3個重複項,所以總計數爲20我的輸出。看起來像一些聯接在這裏如何不合適,但不確定。
有人建議我在這裏錯過什麼?
嗨,http://stackoverflow.com/questions/4441659/linq-distinct-not-working-correctly試試這個 – Niventh 2013-03-28 05:02:10
你有4個外部連接與linq與2與SQL。儘量不要加入你自己。使用導航屬性。 – 2013-03-28 12:23:26
謝謝@GertArnold。正如你所提到的那樣,改變了現在適合我的加入。 – Usher 2013-04-02 05:31:30