, 당신은해야 할 것 같은 뭔가 :
from t in
(
(
from a in source
where somecondition(a)
join b in source2
on a.key equals b.key
where someothercondition(a, b)
select new { a = a, b = b }
).Select((x, i) => new { index = i, a = x.a, b = x.b })
)
select new {
f1 = t.a.f1,
oldf2 = func(t.a.field, t.b.field),
newf2 = func(t.a.field, t.b.field, t.index)
// ... (20 somthing more projected fields) }
하지만 이것은 끔찍한입니다.
a
.Where(a => somecondition(a))
.Join(b, a => a.key, b => b.key, (a,b) => new { a = a, b = b })
.Where(pair => somecondition(pair.a, pair.b))
.Select((t, i) => new
{
f1 = t.a.f1,
oldf2 = func(t.a.field, t.b.field),
newf2 = func(t.a.field, t.b.field, i)
// ...
});
조인 구문은 더 추한,하지만 적어도 당신은 구문을 혼합하지 않는 :
나는 .
형태로 모두 넣어 원합니다.
당신은
var pairs =
from a in source
where somecondition(a)
join b in source2
on a.key equals b.key
where someothercondition(a, b)
select new { a = a, b = b };
var indexedPairs = pairs.Select((x, i) => new { index = i, a = x.a, b = x.b });
var projectedIndexedPairs =
from t in indexedPairs
select new {
f1 = t.a.f1,
oldf2 = func(t.a.field, t.b.field),
newf2 = func(t.a.field, t.b.field, t.index)
// ... (20 somthing more projected fields)
};
을 선호 수도 있지만 그것은 "하나 개의 질의에"아니다 ...
(이 LINQ의 많은입니다, 거기에 몇 가지 구문 오류가있을 수 있지만, 당신은 일반적인 아이디어를 얻을.)
는 영감의 플래시 눈부신
let
의 멘션 마술은 나에게 이것의 thik을 만들었다. 그것은 내가 작성한 빠른 예에서 작동하는 것 같습니다.
// Copious commenting to explain what you're doing.
int x = 0;
// Copious commenting to explain what you're doing.
var query =
from a in source
where somecondition(a)
join b in source2
on a.key equals b.key
where someothercondition(a, b)
let i = x++
select new {
f1 = a.f1,
oldf2 = func(a.field, b.field),
newf2 = func(a.field, b.field, i),
// ... (20 somthing more projected fields)
};
업데이트 : 다소 취약합니다. 예를 들어 query
을 두 번 반복하면 인덱스가 계속 증가합니다. (즉, 0으로 재설정되지 않습니다.)
질문이 명확하지 않습니다. 샘플 코드를 추가하십시오. – jeroenh
LINQ to SQL입니까? –
아니, 그 일반 linq – mmix