我认为问题是由存根函数的返回类型引起的。
你能检查一下你的返回类型吗FilterCustomersByRating
方法在你的 DbContext 中吗?我认为不应该XMLTest
。它应该类似于下面的代码:
[EdmFunction("TestingDbEntities", "FilterCustomersByRating")]
public virtual IQueryable<FilterCustomersByRating_Result> FilterCustomersByRating(Nullable<int> rating)
{
var ratingParameter = rating.HasValue ?
new ObjectParameter("Rating", rating) :
new ObjectParameter("Rating", typeof(int));
return ((IObjectContextAdapter)this)
.ObjectContext
.CreateQuery<FilterCustomersByRating_Result>("[TestingEntities]
.[FilterCustomersByRating](@Rating)", ratingParameter);
}
在这种情况下,存根函数的返回类型将是FilterCustomersByRating_Result
这是当您添加时自动生成的类FilterCustomersByRating
edmx 文件的表值函数。
CREATE FUNCTION [dbo].[FilterCustomersByRating]
(@Rating int)
RETURNS TABLE
AS
RETURN
SELECT XMLTest.*
FROM XMLTest
CROSS APPLY XMLValue.nodes('//MetaData') N(C)
where N.C.value('Rating[1]', 'int')=@Rating
GO
考虑到这一点,您的存根函数应该返回IQueryable<FilterCustomersByRating_Result>
i.e.
[EdmFunction("TestingDbEntities", "FilterCustomersByRating")]
public static IQueryable<FilterCustomersByRating_Result> MyXmlHelper(int rating)
{
throw new NotImplementedException("You can only call this function in a LINQ query");
}
您可以按如下所示使用它:
var dbCustomers = (from x in _context.XMLTests
where MyXmlHelper(1).Any(xh => xh.XMLValue.Contains("1"))
select x);
请注意,虽然这可行,但它会返回所有Customers
。您可能需要修改FilterCustomersByRating
函数接受CustomerID
and rating
.
试一试。
EDIT
除了上述之外,在定义时MyXmlHelper
Edm函数 http://msdn.microsoft.com/en-us/library/system.data.objects.dataclasses.edmfunctionattribute%28v=vs.110%29.aspx,确保拼写FunctionName
and NamespaceName
是正确的。就我而言,FunctionName
is FilterCustomersByRating
and NamespaceName
is TestingEntities
它与自动生成的 DBContext 类中的值匹配。
// </auto-generated code>
public partial class TestingEntities : DbContext
{
public TestingEntities()
: base("name=TestingEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<XMLTest> XMLTests { get; set; }
[EdmFunction("TestingEntities", "FilterCustomersByRating")]
public virtual IQueryable<FilterCustomersByRating_Result> FilterCustomersByRating(Nullable<int> rating)
{
var ratingParameter = rating.HasValue ?
new ObjectParameter("Rating", rating) :
new ObjectParameter("Rating", typeof(int));
return ((IObjectContextAdapter)this)
.ObjectContext
.CreateQuery<FilterCustomersByRating_Result>("[TestingEntities]
.[FilterCustomersByRating](@Rating)", ratingParameter);
}
}