c# - linq中的动态投影与fieldNames列表
问题描述
我想对我的集合的字段和每个实体中的嵌套集合进行投影,我使用 EntityFramework 6.2 我已经通过动态查询来做到这一点,例如下面的代码
Students.Select("new (Name,Family,new(Category.Name) as Category)");
它有效,但是当我想对集合执行此操作时,它会抛出错误
Students.Select("new (Name,Family,new(Courses.Name,Courses.UnitName) as Courses)");
我想要,它返回结果,因为下面的代码返回
Students.Select(std=>new{
std.Name,
Category=new{std.Category.Name},
Courses=std.Courses.Select(co=>new{
co.Name,co.UnitName
})}) ;
如果您有任何想法,请与我分享
解决方案
你不能用System.Linq.Dynamic
. System.Linq.Dynamic.Core
(库的更高级的分支)是可能的。
相当于您以非动态方式编写的查询:
var q = Students.Select("new (Name, Category.Name as Category, Courses.Select(new (Name, UnitName)) as Courses)");
你要找的只是Courses.Select(new (field1, field2)) as SomeAlias
推荐阅读
- regex - 如何删除空白字符并在其位置插入字符串
- python-3.x - 如何使用 flask-sqlachemy 生成以下 json?
- android - 改造android中的certificatePinner
- php - 在ajax中使用while循环
- python - 我正在测试 HTTP 获取方法,但出现错误“没有名为 ASCII 的模块”?
- tomcat - 在netbeans问题中盯着tomcat
- javascript - 如何在从 NodeJs 发送的 React js 中捕获响应以检查数据库中是否已存在电子邮件
- powershell - 无论如何让powershell像典型的自动化服务器一样响应ping?
- ios - 找不到 Plugin.h 文件:flutter 和 xcode
- android - RecyclerView 水平布局上的选定项目隐藏在 Android 上的布局开始之后