无法使用Select(x => Func(x))转换为SQL [英] Cannot Translate to SQL using Select(x => Func(x))

查看:79
本文介绍了无法使用Select(x => Func(x))转换为SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在慢慢学习LINQtoSQL的来龙去脉,但这使我感到困惑.

I'm slowly learning the ins and outs of LINQtoSQL, but this is confusing me.

这是我的陈述:

IQueryable<IEvent> events = (from e in db.getEvents()
                            select e).Select(x => SelectEvent(x, null));

SelectEvent的功能可以在中进行解释.我不使用.toList()函数,因为我不希望将成千上万的记录带入内存.

What the SelectEvent does can be explained in this answer here. I am not using the .toList() function as I don't want potentially thousands of records brought into memory.

public IEvent SelectEvent(SqlServer.Event ev, EventType? type) {
    // Create an object which implements IEvent
    // I don't have the code in front of me, so forgive the lack of code
}

我的问题确实是针对Select()方法的.我收到无法转换为SQL"错误,错误消息中列出了Select().

My question is really for the Select() method. I get the "Cannot translate to SQL" error and the Select() is listed in the error message.

对此一无所知:-/.

推荐答案

Linq到SQL不能将类似的任意函数调用转换为SQL.它不知道该功能做什么.您必须使用表达式构造SelectEvent所做的任何事情.

Linq to SQL cannot translate arbitrary function calls like that to SQL. It has no idea what the function does. You'll have to construct whatever SelectEvent does using expressions.

或者,如果您要做的就是在查询通过数据库后将对象映射到其他对象,则可以在遍历结果时进行操作,或者创建一个为您执行此操作的包装生成器.

Alternatively, if all you want to do is map the objects to something else after the query has gone through the database, just do it as you're looping through the results, or create a wrapping generator that does it for you.

这篇关于无法使用Select(x =&gt; Func(x))转换为SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆