简体   繁体   English

LINQ嵌套查询问题-System.NotSupportedException

[英]LINQ nested query issue - System.NotSupportedException

I'm trying to reproduce the following SQL query in LINQ: 我正在尝试在LINQ中重现以下SQL查询:

select *
  from dbo.events_log 
 where std_datetimeseq < '2014011523:49:00001'
   and logname in ('S', 'D', 'O')
   and std_datetimeseq = (
       select min(std_datetimeseq)
         from dbo.events_log
        where std_datetimeseq < '2014011523:49:00001'
          and logname in ('S', 'D', 'O'))

This is what I have: 这就是我所拥有的:

from e in _context.EventLogs
                    where e.std_datetimeseq.CompareTo(stdDateTimeSeq) < 0
                      && eventMessage.Content.EquipToSearch.Contains(e.logname)
                      && e.std_datetimeseq.CompareTo(
                      ((from e2 in _context.EventLogs
                        where e2.std_datetimeseq.CompareTo(stdDateTimeSeq) < 0
                            && eventMessage.Content.EquipToSearch.Contains(e2.logname)
                        select e2).Min().std_datetimeseq)) == 0
                           select e

I keep getting this exception: 我不断收到此异常:

{"The specified method 'MyEntityModel.EventLog Min[EventLog](System.Linq.IQueryable`1[HDSREntityDataModelNS.EventLog])' 
on the type 'System.Linq.Queryable' cannot be translated into 
a LINQ to Entities store expression because no overload matches the passed arguments."}

I have no idea what's wrong. 我不知道怎么了

eventMessage.Content.EquipToSearch is a List with "S", "D", "O" in it. eventMessage.Content.EquipToSearch是其中包含“ S”,“ D”,“ O”的列表。 The LINQ is going against entity framework. LINQ与实体框架背道而驰。

Thanks 谢谢

The problem is with the sub query 问题在于子查询

(from e2 in _context.EventLogs
 where e2.std_datetimeseq.CompareTo(stdDateTimeSeq) < 0
    && eventMessage.Content.EquipToSearch.Contains(e2.logname)
 select e2).Min().std_datetimeseq

In order to get the min std_datetimeseq value you have to select it, otherwise you are trying to find the minimum EventLogs item which not only doesn't make sense, but cannot be translated to SQL by EF. 为了获得最小的std_datetimeseq值,您必须选择它,否则,您将试图找到最小的EventLogs项,这不仅没有意义,而且不能被EF转换为SQL。 Instead do the following. 而是执行以下操作。

(from e2 in _context.EventLogs
 where e2.std_datetimeseq < stdDateTimeSeq
    && eventMessage.Content.EquipToSearch.Contains(e2.logname)
 select e2.std_datetimese).Min()

On a side note your query will be easier to read if you just use the comparison operators instead of CompareTo as suggested by Servy. 附带一提,如果只使用比较运算符而不是Servy建议的CompareTo ,您的查询将更易于阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM