简体   繁体   English

带有表达式主体的 lambda 表达式无法转换为表达式树 C# 实体框架

[英]A lambda expression with a expression body cannot be converted to an expression tree C# entity framework

I have an entity table with below model我有一个具有以下模型的实体表

   public class VulnerabilityImportFileLog
    {
        public Collection<Vulnerability> Vulnerability { get; set; }
    }

Trying to return an string value using the select statement in the entity framework query尝试使用实体框架查询中的 select 语句返回字符串值

var vulnerabilityImportFileLogSelect = vulnerabilityImportFileLogQuery
        .Select(vulnerabilityImportFileLog =>
            new VulnerabilityImportedScansListViewDto
            {
                AssetName = vulnerabilityImportFileLog.Vulnerability.Select(y =>
                    {
                        if (y.AssetHostIPOrDomain.HostIPAssetId.HasValue)
                            return y.AssetHostIPOrDomain.HostIPAsset.Title;
                        else if (y.AssetHostIPOrDomain.DomainAssetId.HasValue)
                            return y.AssetHostIPOrDomain.DomainAsset.Title;
                        else
                            return y.AssetHostIPOrDomain.HostIPOrDomain;
                    }
                ).Distinct().ToList()
            });

Where AssetName is of type List<string> and getting an exception as A lamda expression with a statement body cannot be converted to an expression tree其中AssetName的类型为List<string>并获得异常为A lamda expression with a statement body cannot be converted to an expression tree

在此处输入图像描述

An suggested answer was to use AsEnumerable() , but even that not solved the issue.建议的答案是使用AsEnumerable() ,但即使这样也不能解决问题。

You can solve the problem by using linq query syntax with 'let' keyword:您可以通过使用带有“let”关键字的 linq 查询语法来解决该问题:

AssetName = (from y in vulnerabilityImportFileLog.Vulnerability
    let resultString = y.AssetHostIPOrDomain.HostIPAssetId.HasValue
      ? y.AssetHostIPOrDomain.HostIPAsset.Title
      : y.AssetHostIPOrDomain.DomainAssetId.HasValue
      ? y.AssetHostIPOrDomain.DomainAsset.Title
      : y.AssetHostIPOrDomain.HostIPOrDomain
  select resultString
)
.ToList()
.Distinct();
    

Assuming vulnerabilityImportFileLogQuery is an IQueryable<vulnerabilityImportFileLog> then a typical way to fetch details from the associated Vulnerability relations would be via a SelectMany .假设脆弱性ImportFileLogQuery 是一个IQueryable<vulnerabilityImportFileLog>那么从关联的漏洞关系中获取详细信息的典型方法是通过SelectMany Given you need to do substitution depending on what is available, one option would be to do a double-projection.鉴于您需要根据可用的内容进行替换,一种选择是进行双重投影。 The first pass selects and materializes just the details we want to inspect, then the second pass composes the results.第一遍选择并实现我们想要检查的细节,然后第二遍组成结果。

vulnerabilityImportFileLogQuery.SelectMany(x => new 
{
    x.Vulnerability.AssetHostIpOrDomain.HostIpOrDomain,
    HostIpAssetTitle = x.Vulnerability.AssetHostIpOrDomain.HostIpAsset.Title,
    DomainAssetTitle = x.Vulnerability.AssetHostIpOrDomain.DomainAsset.Title
}).ToList()
.Select(x => x.HostIpAssetTitle ?? DomainAssetTitle ?? HostIpOrDomain)
.Distinct()
.ToList();

What this essentially does is fetch the 3 values, our default HostIpOrDomain value, and the Titles from the HostIpAsset and DomainAsset if they are available.这实际上是从 HostIpAsset 和 DomainAsset 中获取 3 个值、我们的默认 HostIpOrDomain 值以及 Titles(如果它们可用)。 EF will return #null if the entities aren't associated or the Title is #null.如果实体未关联或 Title 为 #null,则 EF 将返回 #null。 This query is materialized using the ToList , then from that we Select the title to use based on a null check, before taking our Distinct set.这个查询是使用ToList实现的,然后我们根据空检查选择要使用的标题,然后再获取我们的 Distinct 集。

Hopefully that gives you a few ideas as a starting point.希望这能给你一些想法作为起点。

暂无
暂无

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

相关问题 带有语句体的 lambda 表达式无法转换为表达式树(Lambda 和 Linq) - A lambda expression with a statement body cannot be converted to an expression tree(Lambda and Linq) 具有语句主体的lambda表达式无法转换为表达式树 - A lambda expression with a statement body cannot be converted to an expression tree “带有语句体的 lambda 表达式无法转换为表达式树” - "A lambda expression with a statement body cannot be converted to an expression tree" 如何修复“带有语句主体的lambda表达式无法转换为表达式树”以供sql执行? - How to fix "A lambda expression with a statement body cannot be converted to an expression tree” for sql for execution? 解析Enum时发生自动映射器错误:具有语句主体的Lambda表达式无法转换为表达式树 - Automapper error when parsing Enum: A lambda expression with a statement body cannot be converted to an expression tree 实体框架表达式树 lambda 访问者错误 - Entity Framework expression tree lambda visitor error 如何在C#中强制转换表达式树lambda? - How to cast an expression tree lambda in c#? 递归 lambda 表达式遍历 C# 中的树 - Recursive lambda expression to traverse a tree in C# 复杂的OrderBy:语句主体无法转换为表达式树 - Complex OrderBy: Statement body cannot be converted to expression tree 实体框架查询的 C# Linq Lambda 表达式将自定义表达式传递给 Where 条件 - C# Linq Lambda Expression for Entity Framework Query Passing Custom Expression to Where Condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM