在一个linq查询中调用构造函数[英] Invoke constructor inside a linq query

本文是小编为大家收集整理的关于在一个linq查询中调用构造函数的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我需要在 linq 查询中调用构造函数.

我收到此错误:

<块引用>

LINQ to Entities 仅支持无参数构造函数和初始化程序.

这是我的 linq 查询:

IQueryable<Object> list = (from u in db.Object select new Object(u));

这是我的构造函数:

public Object(Object presentation){}

推荐答案

你必须使用没有参数的构造器.

public Object()
{
    public Object Presentation { get; set; }
}

IQueryable list= (from u in db.Object select new Object { Presentation = u });

本文地址:https://www.itbaoku.cn/post/1557174.html

问题描述

I need to invoke a constructor inside of a linq query.

I am getting this error:

Only parameterless constructors and initializers are supported in LINQ to Entities.

Here's my linq query:

IQueryable<Object> list = (from u in db.Object select new Object(u));

Here is my constructor:

public Object(Object presentation){}

推荐答案

You have to use a contructor without parameters.

public Object()
{
    public Object Presentation { get; set; }
}

IQueryable list= (from u in db.Object select new Object { Presentation = u });