Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ to Entities无法识别方法URL.MeaningfulURL,并且无法将此方法转换为存储表达式_C#_Entity Framework_Linq - Fatal编程技术网

C# LINQ to Entities无法识别方法URL.MeaningfulURL,并且无法将此方法转换为存储表达式

C# LINQ to Entities无法识别方法URL.MeaningfulURL,并且无法将此方法转换为存储表达式,c#,entity-framework,linq,C#,Entity Framework,Linq,这个问题特定于接受格式化的url值并与未格式化的数据库值进行比较 我有一个url/公司/我的公司/ 公司名称作为My company存储在数据库字段中-注意:无连字符、空格和大写 它可以像约翰的公司>>那样以更复杂的格式存储,因为用户可以在CMS中输入 那么,我如何将我的公司与我在LINQ的公司进行比较 var type = Context.Set<Domain.Content.Organisation>() .AsNoTracking

这个问题特定于接受格式化的url值并与未格式化的数据库值进行比较

我有一个url/公司/我的公司/

公司名称作为My company存储在数据库字段中-注意:无连字符、空格和大写

它可以像约翰的公司>>那样以更复杂的格式存储,因为用户可以在CMS中输入

那么,我如何将我的公司与我在LINQ的公司进行比较

var type = Context.Set<Domain.Content.Organisation>()
                        .AsNoTracking()
                        .Select(x => new DataContracts.Content.Organisation
                        {
                            Id = x.Id
                            ,Name = x.Name
                            ,OrganisationTypeId = x.OrganisationTypeId
                            ,OrganisationTypeName = (x.OrganisationType != null) ? x.OrganisationType.Name : string.Empty
                            ,Categories = x.Categories.Select(i => i.Id)
                            ,IsCustomer = x.IsCustomer
                            ,CountryId = x.CountryId
                            ,CountryName = (x.Country != null) ? x.Country.Name : string.Empty,
                            SeoName = x.SeoName,
                            Description = x.Description,                           
                            Website = x.Website
                        })
                        .FirstOrDefault(x => x.Id == id || Urls.MeaningfulURL(x.SeoName.ToLower(), null) == seoName.ToLower());
它会生成一个错误:

LINQ to Entities does not recognize the method Urls.MeaningfulURL method, and this method cannot be translated into a store expression.
我明白这意味着什么,但无法找到解决办法

url.MeaningfulURL是一种方法,它通过大量字符串替换来创建url格式“my company”

如何在查询时正确格式化列x.SeoName,使其与url输入匹配

谢谢,EF无法将URL.MeaningfulURL转换为SQL。解决这个问题有很多选择:

一,。首先加载数据:

是的,您可以使用Contains,至少对于SQLServer提供程序是这样

三,。将逻辑移到SQL:

如果使用SQL Server,您可以尝试计算列、视图,甚至其他

四,。将计算值存储在db中:


有时最好存储URL.MeaningfulURLx.SeoName.ToLower,null值,或者与x.SeoName一起存储,并在查询中使用此存储值。此外,还可以在此字段上添加索引。

您不能使用LINQ中的其他函数。您可以添加到数据库表的可计算列中,以计算有意义的URL吗?或者您可以创建返回有意义URL的视图?是的,我认为这必须在数据库级别使用计算列、视图或存储过程来完成。我希望这在临齐是可能的,我会对方案2感兴趣。但是x.seoname可以是任何格式。并且URL输入已经格式化。因此computedString.Contains将不起作用,因为computedString已格式化。你同意吗?我认为选项3是我必须选择的。@fourbeatcoder,如果没有x.seoname值和URL.MeaningfulURL逻辑的例子,很难说。有时,将db模式更改为存储计算的URL会更好。MeaningfulURLx.SeoName.ToLower,null而不是原始的x.SeoName我想我应该添加这个选项来回答。是的,选项4也是可行的。非常感谢。
LINQ to Entities does not recognize the method Urls.MeaningfulURL method, and this method cannot be translated into a store expression.
var type = Context.Set<Domain.Content.Organisation>()
    .AsNoTracking()
    .Select(your select)
    .ToList()
    .FirstOrDefault(x => x.Id == id || Urls.MeaningfulURL(x.SeoName.ToLower(), null) == seoName.ToLower());
var type = Context.Set<Domain.Content.Organisation>()
    .AsNoTracking()
    .Select(your select)
    .FirstOrDefault(x => x.Id == id || computedString.Contains(x.SeoName));