I think the problem is caused by the return type of your stub function.
Can you check what type of return for your FilterCustomersByRating method is in your DbContext? I do not think this should be an XMLTest . It should look like below:
[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); }
In this case, the return type of the stub function will be of type FilterCustomersByRating_Result , which is automatically generated when the FilterCustomersByRating Table-valued function is added to your edmx file.
CREATE FUNCTION [dbo].[FilterCustomersByRating] (@Rating int) RETURNS TABLE AS RETURN SELECT XMLTest.* FROM XMLTest CROSS APPLY XMLValue.nodes('//MetaData') N(C) where NCvalue('Rating[1]', 'int') =@Rating GO
Given this, your stub function should be returned by IQueryable<FilterCustomersByRating_Result> ..
[EdmFunction("TestingDbEntities", "FilterCustomersByRating")] public static IQueryable<FilterCustomersByRating_Result> MyXmlHelper(int rating) { throw new NotImplementedException("You can only call this function in a LINQ query"); }
You can use it as below:
var dbCustomers = (from x in _context.XMLTests where MyXmlHelper(1).Any(xh => xh.XMLValue.Contains("1")) select x);
Please note that while this will work, it will return all Customers . You may need to change the FilterCustomersByRating function to accept CustomerID and rating .
Give it a try.
EDIT
In addition to the above, when defining MyXmlHelper EdmFunction, make sure that the spelling of FunctionName and NamespaceName is correct. In my case, FunctionName is FilterCustomersByRating , and NamespaceName is TestingEntities , which correspond to the values ββin the automatically generated DBContext class.
// </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); } }