我有两个表,提交和记谱。 提交内容包含单个条目,符号包含通过 AppID 链接的提交内容的多个条目。
我试图完成的是提取给定日期范围的提交,以及最近的提交。
我已设法获取所有数据;但是,它为每个提交记录复制了每个提交记录。
这是我目前的代码。任何人都可以帮我解决这个问题吗?
var query = (from s in db.Submissions
from n in db.notations
from d in db.DCodes
where s.AppID == (int) n.AppID
&& s.DCode == d.DCode1
&& s.received >= dts
&& s.received <= dte
select new ApplicationTrackingSystem.customModels.Export
{
AppID = s.AppID,
received = s.received,
//dcode = s.DCode,
dcode = d.description,
firstName = s.firstName,
middleName = s.middleName,
lastName = s.lastName,
street = s.street,
city = s.city,
state = s.state,
zip = s.zip,
position = s.position,
hearAbout = s.hearAbout,
referredby = s.referredBy,
email = s.email,
commentID = n.commentID,
commentDate = n.commentDate,
user = n.userID,
comment = n.comment
}).ToList();
答案 0 :(得分:2)
沿着这些方向尝试一些事情。 分组提交的符号,然后仅commentDate
检索第一次降序排序:
var result = from s in db.Submissions
join n in db.notations on s.AppID equals n.AppID
where s.received >= dts && s.received <= dte
group n by s into g
let recentNotation = g.OrderDescendingBy(item => item.commentDate).First()
select new ApplicationTrackingSystem.customModels.Export {
AppID = g.Key.AppId,
/* rest of s fields */,
commentID = recentNotation.commentID,
commentDate = recentNotation.commentDate,
user = recentNotation.userID,
comment = recentNotation.comment
};
当Ivan纠正我时,使用GroupJoin
更好:
var result = from s in db.Submissions
join n in db.notations on s.AppID equals n.AppID into g
from n in g.OrderDescendingBy(item => item.commentDate).First()
where s.received >= dts && s.received <= dte
select new ApplicationTrackingSystem.customModels.Export {
AppID = g.Key.AppId,
/* rest of s fields */,
commentID = n.commentID,
commentDate = n.commentDate,
user = n.userID,
comment = n.comment
};