Consultar Xml desde SQL usando Entity Framework Database Primero
Necesito usar Entity Framework, LINQ para consultar los datos XML del SQL en mi aplicación asp.net mvc (C #).
Tengo una columnaXMLValue
con datos
<MetaData>
<Reviews>1</Reviews>
<Rating>1</Rating>
</MetaData>
Necesito obtener todo elCustomers
quien tiene unRating
de 1 del xml. Me he referidoesta publicación de stackoverflow y no puedo lograrlo.
He agregado la función SQL y la agregué a mi 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
Y la siguiente función DB:
[DbFunction("XMLDBModel.Store", "FilterCustomersByRating")] public static IQueryable<XMLTest> MyXmlHelper(int rating) { throw new NotImplementedException("You can only call this function in a LINQ query"); }
Below is the linq query which I tried exactly as in the post, but not able to use the function and it throws error.
var _dbCustomers = (from x in _context.XMLTests where MyXmlHelper(1).Where(xh=> xh.XMLValue.Contains("1")) select x);
Error:
Cannot implicitly convert type 'System.Linq.IQueryable<XMLTest>' to 'bool
If I user Any(), I have the following error:
var _dbCustomers = (from x in _context.XMLTests where MyXmlHelper(1).Any(xh => xh.XMLValue.Contains("1")) select x);
Error:
The specified method 'System.Linq.IQueryable`1[XMLTest] MyXmlHelper(Int32)' on the type 'CustomerRepository' cannot be translated into a LINQ to Entities store expression because its return type does not match the return type of the function specified by its DbFunction attribute.
Can someone suggest on how to achieve this please?