2016-11-11 14 views
3

これは、LINQクエリに変換する必要があるSQLクエリです。私は、LINQクエリで、これまで行われてきた何SQLクエリをLINQクエリに変換する

SELECT pq.DocumentQueueID, 
    pq.DocumentQueueName, 
    pq.DepartmentName, 
    pq.UserLocation, 
    ISNULL(T.DocumentCount, 0) DocCount, 
    ISNULL(CONVERT(VARCHAR(50),T.OldestDocumentDate),'') IngestionDateTime, 
    ISNULL(B.UserName, '') UserName 
FROM [dbo].[listPLDQueues] pq 
LEFT OUTER JOIN 
(
    SELECT dds.CurrentDocumentQueue, 
    SUM(dds.ImportPageCount) as DocumentCount, 
    MIN(dds.IngestionDateTime) as OldestDocumentDate 
    FROM [dbo].[dataDocumentStats] dds 
    GROUP BY dds.CurrentDocumentQueue 
) AS T ON T.CurrentDocumentQueue = pq.DocumentQueueID 
LEFT OUTER JOIN 
( SELECT duq.DocumentQueueID, UserName = 
    STUFF((SELECT ', ' + uq.UserDisplayName 
    FROM [dbo].[dataUserQueues] uq 
    WHERE uq.DocumentQueueID = duq.DocumentQueueID 
    FOR XML PATH('')),1,2,'') 
FROM [dbo].[dataUserQueues] duq 
GROUP BY duq.DocumentQueueID 
) AS B ON B.DocumentQueueID = pq.DocumentQueueID 
WHERE UPPER(WorkflowType) = 'INDEXING' 

..

var indexSummary = _eimStatsDB.listPLDQueues 
      .Join(_eimStatsDB.dataDocumentStats, 
       pld => pld.DocumentQueueID, 
       dds => dds.CurrentDocumentQueue, 
       (pld, dds) => new { pldQueues = pld, dataDocument = dds })    
      .Where(a => a.pldQueues.WorkflowType.ToLower() == "indexing") 
      .GroupBy(a => a.pldQueues.DocumentQueueID) 
      .ToList() 
      .Select(a => new 
      { 
       DocumentQueueId = a.Key, 
       DocumentQueueName = a.Select(i => i.pldQueues.DocumentQueueName).FirstOrDefault(), 
       DepartmentName = a.Select(i => i.pldQueues.DepartmentName).FirstOrDefault(), 
       DocumentCount = a.Sum(i => i.dataDocument.ImportPageCount), 
       OldestDocumentDate = a.Min(i => i.dataDocument.IngestionDateTime), 
       UserLocation = a.Select(i => i.pldQueues.UserLocation).FirstOrDefault(), 
       IsChecked = false 
      }); 

     var userNames = _eimStatsDB.dataUserQueues 
      .GroupBy(e => e.DocumentQueueID) 
      .ToList() 
      .Select(e => new 
      { 
       DocumentId = e.Key, 
       UserName = string.Join(",", e.Select(i => i.UserDisplayName)) 
      }); 

     var listPLDQueue = from pldqueue in _eimStatsDB.listPLDQueues 
          where pldqueue.WorkflowType == "Indexing" 
          select pldqueue; 

     var result = from pldqueue in listPLDQueue 
        join iS in indexSummary 
        on pldqueue.DocumentQueueID equals iS.DocumentQueueId into pldjoin 
        from pld in pldjoin.DefaultIfEmpty() 
        join un in userNames 
        on pld.DocumentQueueId equals un.DocumentId into gj 
        from subuser in gj.DefaultIfEmpty() 
        select new 
        { 
         DocumentQueueId = pld.DocumentQueueId, 
         DocumentQueueName = pld.DocumentQueueName, 
         DepartmentName = pld.DepartmentName, 
         DocumentCount = (pld.DocumentCount == null ? 0 : pld.DocumentCount), 
         OldestDocumentDate = (pld.OldestDocumentDate == null? Convert.ToDateTime(string.Empty) : pld.OldestDocumentDate), 
         UserLocation = pld.UserLocation, 
         IsChecked = pld.IsChecked, 
         Usernames = (subuser == null ? string.Empty : subuser.UserName) 
        }; 

結果を返す最後のクエリがエラーを与える:。 「タイプの一定の値を作成できません 『匿名型』のみこのコンテキストでは、プリミティブ型または列挙型がサポートされています。

すべての異なるLINQクエリを組み合わせることができる場合は、これを実現する他の方法がありますか?

+0

ていますか?私は問題があなたのjoinクエリに 'result'を割り当てるときに関係していると思います。 –

+0

はい、結果を割り当てるときにエラーが発生しています。それはわかりません。 –

+0

NotSupportedExceptionをスローするメモリに格納されたコレクションを使用してデータベースオブジェクトに参加しようとしています。 IEnumerable型のパラメータがプリミティブ型( 'int'など)であればEFは結合を参照するだけなので、' Select'文の前に 'toList()'メソッドを 'userNames'宣言から削除してみてください。しかし' IQueryable'をそうする。 –

答えて

1

クエリ全体の構造を調べた後、私はlistPLDQueue以外の他の2 join源を発見したが、結合操作を実行するときに型パラメータまたはIQueryableなどのプリミティブ型でIEnumerableを参照するEntity Frameworkのは、唯一可能な匿名型のパラメータを持つIEnumerable集まりです。

てみ落ちたり、代わりに次のように匿名型を使用して、適切なクラス名を与えることを検討、indexSummaryuserNamesの両方にIQueryableを割り当てるために、すべてのToList()方法をコメント:

var indexSummary = _eimStatsDB.listPLDQueues 
      .Join(_eimStatsDB.dataDocumentStats, 
       pld => pld.DocumentQueueID, 
       dds => dds.CurrentDocumentQueue, 
       (pld, dds) => new { pldQueues = pld, dataDocument = dds })    
      .Where(a => a.pldQueues.WorkflowType.ToLower() == "indexing") 
      .GroupBy(a => a.pldQueues.DocumentQueueID) 
     //.ToList() --> this converts IQueryable to IEnumerable, which should be dropped 
      .Select(a => new listPLDQueues() // change this assignment to your model class name 
      { 
       DocumentQueueId = a.Key, 
       DocumentQueueName = a.Select(i => i.pldQueues.DocumentQueueName).FirstOrDefault(), 
       DepartmentName = a.Select(i => i.pldQueues.DepartmentName).FirstOrDefault(), 
       DocumentCount = a.Sum(i => i.dataDocument.ImportPageCount), 
       OldestDocumentDate = a.Min(i => i.dataDocument.IngestionDateTime), 
       UserLocation = a.Select(i => i.pldQueues.UserLocation).FirstOrDefault(), 
       IsChecked = false 
      }); 

var userNames = _eimStatsDB.dataUserQueues 
      .GroupBy(e => e.DocumentQueueID) 
      //.ToList() --> this converts IQueryable to IEnumerable, which should be dropped 
      .Select(e => new dataUserQueues() // change this assignment to your model class name 
      { 
       DocumentId = e.Key, 
       UserName = string.Join(",", e.Select(i => i.UserDisplayName)) 
      }); 

各割り当ては、Tが割り当てられている(IQueryable<T>を返します。 DBモデルクラス名、すなわちIQueryable<listPLDQueues>およびIQueryable<dataUserQueues>)を使用して、の割り当てにjoinというクエリを含めるのに適しています。

関連の問題&参照:エラーが発生したライン

Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context

Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context two db Linq query

IQueryable for Anonymous Types

関連する問題