我有一个工作的sql语句,并希望它作为linq语句或linq方法链。
我的陈述是:
SELECT T1.*
FROM (SELECT Col1, MAX(InsertDate) as Data
FROM Zugbewegungsdaten
GROUP BY Loknummer) as temp JOIN T1
ON (T1.Col1= temp.Col1
AND Zugbewegungsdaten.InsertDate= temp.Date)
WHERE Col3=1
ORDER BY Loknummer
有人可以帮我翻译吗?
评论后修改:
好的,我的内部选择结果:
var maxResult = (from data in context.T1
group data by data.Col1
into groups
select new
{
Train = groups.Key,
InsertDate= groups.Max( arg => arg.InsertDate)
}) ;
我尝试了这样的连接:
var joinedResult = from data in context.T1
join gdata in maxResult on new
{
data.Col1,
data.InsertDate
}
equals new
{
gdata.Col1,
gdata.InsertDate
}
select data;
但是我通过连接得到了一个编译错误,即typeargument无效。
如果连接有效,我会使用where筛选joinedResult。
var result = from data in joinedResult
where data.Col3 == true
select data;
答案 0 :(得分:0)
经过更多“尝试和错误”之后,我得到了这个版本它看起来很有效。
var joinedResult = ( ( from data in context.T1
group data by data.Col1
into g
select new
{
Key= g.Key,
Date = g.Max( p => p.InsertDate)
} ) ).Join( context.T1,
temp => new
{
Key = temp.Key,
InsertDate = temp.Date
},
data => new
{
Key = data.Col1,
InsertDate = data.InsertDate
},
( temp,
data ) => new
{
temp,
data
} )
.Where( arg => arg.data.Col3)
.OrderBy( arg => arg.data.Col1)
.Select( arg => arg.data );
难道我必须通过多列连接设置相同的属性名称(Key,InsertDate)吗?