使用动态 Linq Select 表达式的构造函数调用
Constructor Invocation using Dynamic Linq Select Expression
我正在使用 Dynamic Linq。
我可以做到以下几点:
IQueryable<Person>().Select("new(Id as Id, FirstName + Surname as Name");
这将创建一个包含匿名类型的 IQueryable
( 而不是 IQueryable<object>
)。
但鉴于我有以下 class:
public class ListItem
{
public ListItem()
{
}
public ListItem(int id, string name)
{
this.Id = id;
this.Name = name;
}
public int Id { get; set; }
public string Name { get; set; }
}
我想通过调用 ListItem
的构造函数,从我的 IQueryable<Person>
直接 select 进入此类型,等同于以下内容:
IQueryable<Person>().Select(p => new ListItem(p.Id, p.FirstName + p.Surname));
我尝试过以下变体:
// Illegal - new expects form "new()" - exception "'(' expected"
IQueryable<Person>().Select("new ListItem(Id, FirstName + Surname)");
// this doesn't work, "No property 'ListItem' exists on type 'Person'"
IQueryable<Person>().Select("ListItem(Id, FirstName + Surname)");
// this doesn't work because "[" is not expected
IQueryable<Person>().Select("[MyProject.Utils.ListItem,MyProject]()");
我相信从文档中可以做到这一点 available here:
Overload resolution for methods, constructors, and indexers uses rules similar to C#. In informal terms, overload resolution will pick the best matching method, constructor, or indexer, or report an ambiguity error if no single best match can be identified.
Note that constructor invocations are not prefixed by new.
这是可能的还是我误解了文档?
我不确定 Dynamic Linq 中的 Select
方法是否支持构造函数。也许文档说这仅支持谓词,例如 Where
方法中使用的表达式。不过我不确定。
无论如何,这可能是适合您的解决方案:
var result =
context
.Customers
.Select("new(Id as Id, FirstName + Surname as Name")
.AsEnumerable()
.Select(x => new ListItem(x.Id, x.Name))
.ToList();
我正在使用 Dynamic Linq。
我可以做到以下几点:
IQueryable<Person>().Select("new(Id as Id, FirstName + Surname as Name");
这将创建一个包含匿名类型的 IQueryable
( 而不是 IQueryable<object>
)。
但鉴于我有以下 class:
public class ListItem
{
public ListItem()
{
}
public ListItem(int id, string name)
{
this.Id = id;
this.Name = name;
}
public int Id { get; set; }
public string Name { get; set; }
}
我想通过调用 ListItem
的构造函数,从我的 IQueryable<Person>
直接 select 进入此类型,等同于以下内容:
IQueryable<Person>().Select(p => new ListItem(p.Id, p.FirstName + p.Surname));
我尝试过以下变体:
// Illegal - new expects form "new()" - exception "'(' expected"
IQueryable<Person>().Select("new ListItem(Id, FirstName + Surname)");
// this doesn't work, "No property 'ListItem' exists on type 'Person'"
IQueryable<Person>().Select("ListItem(Id, FirstName + Surname)");
// this doesn't work because "[" is not expected
IQueryable<Person>().Select("[MyProject.Utils.ListItem,MyProject]()");
我相信从文档中可以做到这一点 available here:
Overload resolution for methods, constructors, and indexers uses rules similar to C#. In informal terms, overload resolution will pick the best matching method, constructor, or indexer, or report an ambiguity error if no single best match can be identified.
Note that constructor invocations are not prefixed by new.
这是可能的还是我误解了文档?
我不确定 Dynamic Linq 中的 Select
方法是否支持构造函数。也许文档说这仅支持谓词,例如 Where
方法中使用的表达式。不过我不确定。
无论如何,这可能是适合您的解决方案:
var result =
context
.Customers
.Select("new(Id as Id, FirstName + Surname as Name")
.AsEnumerable()
.Select(x => new ListItem(x.Id, x.Name))
.ToList();