将SQL查询转换为LINQ查询

时间:2016-11-11 00:15:46

标签: c# sql-server entity-framework linq linq-to-entities

这是需要转换为LINQ查询的SQL查询。

mydata <- data.frame(id =c(1,2,3), 
                     path= c("a,b", "c,a", "b"),
                     events =c (("2,3"), ("3,4"), ("5")))

到目前为止我在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'

返回结果的最后一个查询会出错: “无法创建'匿名类型'类型的常量值。在此上下文中仅支持基本类型或枚举类型。”

还有其他更好的方法可以将所有不同的LINQ查询组合在一起吗?

1 个答案:

答案 0 :(得分:1)

在检查整个查询结构后,我发现除了join之外的其他两个listPLDQueue来源都是具有匿名类型参数的IEnumerable个集合,其中实体框架只能引用IEnumerable原始类型作为类型参数或执行连接操作时IQueryable

尝试删除或评论所有ToList()方法,为IQueryableindexSummary分配userNames,然后考虑使用匿名类型提供正确的类名:

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))
            });

每个作业都会返回IQueryable<T>(T分配给数据库模型类名称,即IQueryable<listPLDQueues>IQueryable<dataUserQueues>),这些名称适合在包含{result的{​​{1}}作业中使用1}}查询。

相关问题&amp;参考文献:

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