2009-07-22 7 views
1

어떻게 다음 SQL 문을 LinqToSQL 문으로 변환 할 수 있습니까?SQL 문을 Linq로 변환

select field, 1 as ordering from table where field2 = condition1 
union all 
select field, 7 as ordering from table where field2 = condition2 
union all 
select field, 3 as ordering from table where field2 = condition3 
union all 
select field, 2 as ordering from table where field2 = condition4 
order by ordering 

실제로 몇 가지 쿼리에 참여하고 행의 출처를 기반으로 결과 집합을 정렬합니다.

다음과 같이 공용체를 관리 할 수 ​​있지만 LinqToSQL을 전체 결과 집합으로 정렬 할 수는 없지만 각 개별 쿼리를 주문할 수만 있습니다. SQL에

from t in table 
where 
condition 
select new { field, ordering = 1 } 
).Union 
(
from t2 in table2 
where 
condition 
select new { field ordering = 7 } 
).Union 
(
from t3 in table3 
where 
condition 
select new { field ordering = 3 } 
).Union 
(
from t4 in table4 
where 
condition 
select new { field ordering = 2 } 
); 

답변

0

LINQ 때문에 성능 저하하지 않아야 추가로 문을 메모리에 재정렬 필요에 따라 전선에 단지 많은 데이터를 얻을 도움이 될 것입니다. 물론 이번에는 Objects에 대한 LINQ가 될 것입니다.

마지막 유니온 이후에 OrderBy() 또는 OrderByDescending() 번으로 전화를 걸어 보았습니까?

+0

그건 정확히 내가 한 것입니다. - 고마워요. – user9659

0

두 가지 생각; UNION ALL이면 .Concat(...).Union(...)보다 적절합니다.

둘째로; 이 후 메모리에 주문하여 할 것입니다

var origQry = ... 

var qry = origQry.AsEnumerable().OrderBy(x => x.ordering); 

: 당신은 LINQ - 투 - 객체 필요한 경우에 별도로 주문을 할 수 있습니다.

관련 문제