简体   繁体   English

方法名称(int,DateTime)不支持SQL转换

[英]Method Name(int, DateTime) has no supported translation to SQL

I have a function, Name(int, DateTime) , that I'd like to use in a linq2sql query. 我有一个函数Name(int, DateTime) ,我想在linq2sql查询中使用它。

Its a simple function that computes the difference between the passed in DateTime object and DateTime.Now using a TimeSpan . 它是一个简单的函数,它使用TimeSpan计算传入的DateTime对象和DateTime.Now之间的差。 I then do some basic math stuff with .TotalHours and the passed in int. 然后,我使用.TotalHours和传入的int进行一些基本的数学运算。 The most complicated function is Math.Pow(double,double). 最复杂的函数是Math.Pow(double,double)。

public static double Name(int num, DateTime Timestamp)
{
        DateTime now = DateTime.Now;
        DateTime then = Timestamp;

        TimeSpan elapsedSpan = new TimeSpan(now.Ticks - posted.Ticks);
        double t = elapsedSpan.TotalHours;

        return System.Math.Pow(t, num);
}

I get this error: 我收到此错误:

Method 'Double Name(Int32, System.DateTime)' has no supported translation to SQL. 

When running a query like this: 当运行这样的查询时:

var q = from k in db.Stuff
        select new {Name=Name(k.num, k.Timestamp)};

I understand that the function cannot be converted to SQL as it currently is written. 据我了解,该函数当前无法写入SQL。 Is there a way to convert what I have into something that is SQL useable? 有没有一种方法可以将我所拥有的转换为SQL可用的东西?

I should note that a ToList() or ToArray() method is not useable because the datatable is large. 我应该注意,因为数据表很大,所以ToList()或ToArray()方法不可用。

As your conversion is only use when projecting, I suggest that you simply do that bit in .NET instead of in SQL: 由于您的转换仅在投影时使用,因此建议您仅在.NET中而不是在SQL中执行该操作:

var q = db.Stuff.Select(k => new { k.num, k.Timestamp })
                .AsEnumerable() // Do further processing in .NET
                .Select(k => Name(k.num, k.Timestamp));

Note that I've taken the result out of an anonymous type too, so the type of q will just be an IEnumerable<double> . 请注意,我也从匿名类型中取出了结果,因此q的类型将只是IEnumerable<double>

If you really need the processing to be done in the database, I suggest you write a user-defined function in SQL yourself and tell LINQ to SQL about that (or make it part of a view that you select from). 如果您确实需要在数据库中完成处理,建议您自己用SQL编写一个用户定义的函数,然后将它告诉LINQ to SQL(或使其成为您选择的视图的一部分)。

LINQ-to-SQL can't translate arbitrary code methods, as they are IL (it expects "expression trees", which is what lambdas and LINQ query syntax a generate). LINQ-to-SQL无法转换任意代码方法,因为它们是IL(它期望“表达式树”,这是lambda和LINQ查询语法生成的)。 If you only need this logic in your .NET code, switch to LINQ-to-Objects before calling, Ie 如果仅在.NET代码中需要此逻辑,请在调用Ie之前切换到LINQ-to-Objects

var qry = some complex LINQ query

var data = from row in qry.AsEnumerable()
            select new {
               row.Id,
               Name = YourType.Name(row.Foo, row.Bar)
            };

The AsEnumerable() breaks the LINQ composition in two, allowing your managed code to run as normal. AsEnumerable()将LINQ组成分为两部分,使您的托管代码能够正常运行。

If you need to do something similar at the database , then write the code in TSQL as a UDF, then map the UDF into LINQ-to-SQL by dragging it onto the designer. 如果您需要在数据库上执行类似的操作 ,则以TSQL的形式将代码作为UDF编写,然后通过将UDF拖到设计器上将其映射到LINQ-to-SQL。 You can then access it as a new method on the data-context, Ie 然后,您可以作为数据上下文上的新方法来访问它,即

 select db.Name(row.Foo, row.Bar)

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

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