2009-05-07 5 views
2

오후에 다음 쿼리를 LINQ로 변환하는 방법에 대한 생각을 감추려 고 노력했지만 시간이 많이 걸렸습니다.LINQ를 사용하는 하위 쿼리의 고급 다중 조인

declare @productId int; set @productId = 3212; 

select * from InformationData data where productId = @productId and orgId = 1 
and exists(
    select id from (
     select coalesce(id1.id, id2.id, id3.id) as id from (
      select productId,attributeId from InformationData where productId = @productId group by productId,attributeId 
     ) id 
     left outer join InformationData id1 on id1.productId = id.productId and id1.attributeId = id.attributeId and id1.language = 1 
     left outer join InformationData id2 on id2.productId = id.productId and id2.attributeId = id.attributeId and id2.language = 2 
     left outer join InformationData id3 on id3.productId = id.productId and id3.attributeId = id.attributeId and id3.language = 0 
    ) row 
    where row.id = data.id 
) 

쿼리의 목적은 데이터 언어 (1)에 존재하지 않는, 그래서 만약이 개 대체 언어를 사용하여 테이블에서 데이터를 가져 오기 위해, 그것은 언어 2 인출하고, 2 않는 경우되지 그것이 존재 전역 변환 인 언어 0을 가져 왔습니다.

나는 주로 올바른 내부 쿼리를 얻을 수 있습니다 (id1.language = 1, 나는 내가 어떤 아이디어에 가입하고있어 테이블의 회원에 가입 얻을 수없는 것 제외?)

다음 SQL 생성

(
    from data in (
     from d in InformationData where d.ProductId == 3212 group d by new { d.ProductId, d.AttributeId } into p select new { ProductId = p.Key.ProductId, AttributeId = p.Key.AttributeId } 
    ) 
    join x1 in InformationData on new { a = data.ProductId, b = data.AttributeId } equals new { a = x1.ProductId, b = x1.AttributeId } into f1 
     from r1 in f1.DefaultIfEmpty() 
     where r1.Language == 1 
    join x2 in InformationData on new { a = data.ProductId, b = data.AttributeId } equals new { a = x2.ProductId, b = x2.AttributeId } into f2 
     from r2 in f2.DefaultIfEmpty() 
     where r2.Language == 2 
    join x3 in InformationData on new { a = data.ProductId, b = data.AttributeId } equals new { a = x3.ProductId, b = x3.AttributeId } into f3 
     from r3 in f3.DefaultIfEmpty() 
     where r3.Language == 2 
    select new { Id = ((int?)r1.Id) ?? ((int?)r2.Id) ?? r3.Id } 
).Dump(); 

:

이 내 코드 (LINQPad 코드)입니다

-- Region Parameters 
DECLARE @p0 Int SET @p0 = 3212 
DECLARE @p1 Int SET @p1 = 2 
DECLARE @p2 Int SET @p2 = 2 
DECLARE @p3 Int SET @p3 = 1 
-- EndRegion 
SELECT COALESCE([t2].[id],COALESCE([t3].[id],[t4].[id])) AS [Id] 
FROM (
    SELECT [t0].[productId], [t0].[attributeId] 
    FROM [InformationData] AS [t0] 
    WHERE [t0].[productId] = @p0 
    GROUP BY [t0].[productId], [t0].[attributeId] 
    ) AS [t1] 
LEFT OUTER JOIN [InformationData] AS [t2] ON ([t1].[productId] = [t2].[productId]) AND ([t1].[attributeId] = [t2].[attributeId]) 
LEFT OUTER JOIN [InformationData] AS [t3] ON ([t1].[productId] = [t3].[productId]) AND ([t1].[attributeId] = [t3].[attributeId]) 
LEFT OUTER JOIN [InformationData] AS [t4] ON ([t1].[productId] = [t4].[productId]) AND ([t1].[attributeId] = [t4].[attributeId]) 
WHERE ([t4].[language] = @p1) AND ([t3].[language] = @p2) AND ([t2].[language] = @p3) 

하지만 쿼리의 나머지와 함께이를 넣을 수 없습니다를, 어쩌면 난 그냥 해요 피곤한 구매 나는 계속 내가 CROSS APPLY가 많이 있습니다. 누구든지 어떤 제안이 있습니까?

답변

3

을 일부 사각 사각 물건은 조금 더 밝은 있고 난은 발견 예를 들어, 우선 순위가 무엇 다음, 다음, 다음 2, 3, 1 할 필요가있는 경우, 당신은이 작업을 수행 할 수 호기심있는 사람들을위한 솔루션 : 여기가


(
    from i in InformationData 
    where (
     from data in (
      from d in InformationData where d.ProductId == 3212 group d by new { d.ProductId, d.AttributeId } into p select new { ProductId = p.Key.ProductId, AttributeId = p.Key.AttributeId } 
     ) 
     join x1 in InformationData on new { a = data.ProductId, b = data.AttributeId, c = 1} equals new { a = x1.ProductId, b = x1.AttributeId, c = x1.Language } into f1 
      from r1 in f1.DefaultIfEmpty() 
     join x2 in InformationData on new { a = data.ProductId, b = data.AttributeId, c = 2 } equals new { a = x2.ProductId, b = x2.AttributeId, c = x2.Language } into f2 
      from r2 in f2.DefaultIfEmpty() 
     join x3 in InformationData on new { a = data.ProductId, b = data.AttributeId, c = 0 } equals new { a = x3.ProductId, b = x3.AttributeId, c = x3.Language } into f3 
      from r3 in f3.DefaultIfEmpty() 
     select new { Id = ((int?)r1.Id) ?? ((int?)r2.Id) ?? r3.Id } 
    ).Any(d => d.Id == i.Id) 
    select i 
).Dump(); 

입니다 그리고 여기가 1 폴백 (fallback)을 필요로하는 곳에 나는 경우가 수


-- Region Parameters 
DECLARE @p0 Int SET @p0 = 3212 
DECLARE @p1 Int SET @p1 = 1 
DECLARE @p2 Int SET @p2 = 2 
DECLARE @p3 Int SET @p3 = 0 
-- EndRegion 
SELECT [t0].[id] AS [Id], [t0].[attributeId] AS [AttributeId], [t0].[productId] AS [ProductId], [t0].[value] AS [Value], [t0].[orgId] AS [OrgId], [t0].[version] AS [Version], [t0].[language] AS [Language], [t0].[metaType] AS [MetaType], [t0].[overload] AS [Overload], [t0].[parentId] AS [ParentId] 
FROM [InformationData] AS [t0] 
WHERE EXISTS(
    SELECT NULL AS [EMPTY] 
    FROM (
     SELECT COALESCE([t3].[id],COALESCE([t4].[id],[t5].[id])) AS [value] 
     FROM (
      SELECT [t1].[productId], [t1].[attributeId] 
      FROM [InformationData] AS [t1] 
      WHERE [t1].[productId] = @p0 
      GROUP BY [t1].[productId], [t1].[attributeId] 
      ) AS [t2] 
     LEFT OUTER JOIN [InformationData] AS [t3] ON ([t2].[productId] = [t3].[productId]) AND ([t2].[attributeId] = [t3].[attributeId]) AND (@p1 = [t3].[language]) 
     LEFT OUTER JOIN [InformationData] AS [t4] ON ([t2].[productId] = [t4].[productId]) AND ([t2].[attributeId] = [t4].[attributeId]) AND (@p2 = [t4].[language]) 
     LEFT OUTER JOIN [InformationData] AS [t5] ON ([t2].[productId] = [t5].[productId]) AND ([t2].[attributeId] = [t5].[attributeId]) AND (@p3 = [t5].[language]) 
     ) AS [t6] 
    WHERE [t6].[value] = [t0].[id] 
    ) 
2

주로 쿼리가 수행해야하는 작업에 대한 설명을 바탕으로 적절한 "orderby"절을 사용하여 동일한 결과를 얻고 첫 번째 결과 만 검색 할 수 있다고 생각합니다. 이처럼 :

var result = 
(
    from d in InformationData 
    where d.ProductId == 3212 
    orderby ((d.language == 0) ? Int32.MaxValue : d.language) 
    select d 
).First(); 

편집 : 당신은 ORDERBY 절을 확장하여 검색 우선 순위를 제어 할 수 있습니다. 좋은 밤 잠을 잘 후에

var result = 
(
    from d in InformationData 
    where d.ProductId == 3212 
    orderby 
     (d.language == 2) ? 0 
     : (d.language == 3) ? 1 
     : (d.language == 1) ? 2 
     : Int32.MaxValue 
    select d 
).First(); 
+0

는 불행하게도이 간단하지 생성 된 SQL입니다 -> 2 -> 0 또는 3 -> 2 -> 0. 그것은 오름차순 또는 내림차순 정렬하려면 더 습기에 조건을 기반으로 정렬을 만들 수 있습니다, 나는 그것을 시도 줄 것입니다. 나는 많은 질문이있는 다소 복잡한 응용 프로그램을 번역하고 있기 때문에 여전히 이것을 해결하는 방법에 대해 매우 궁금합니다. – Runeborg

+0

더 복잡한 우선 순위/대체 시퀀스의 예제를 추가했습니다. –

+0

좋은 제안, 이것을 시도해 보겠습니다. 현재는 대기 시간이 길어 프로파일 링 할 수 없습니다. – Runeborg