Attualmente sto convertendo le funzioni nel DAL di una vecchia app in una nuova app utilizzando Entity Framework / LINQ nel suo DAL.
Ci sono alcuni casi in cui vorrei passare direttamente una stringa di SQL al database. È possibile quando usi LINQ? Ecco cosa ho provato con la ricerca ma ExecuteQuery non è disponibile.
using (var context = new DbContext())
{
var sql = @"SELECT DISTINCT * FROM Customer where CustomerId = {0}";
sql = string.Format(sql, customerId);
var query = DbContext.ExecuteQuery<Customer>(sql);
return query.ToList();
}
Questo sembra abbastanza semplice, ma ExecuteQuery non è disponibile per me.
Ecco il mio prossimo tentativo che sembra molto meglio: (per favore dimmi se c'è un modo migliore)
StringBuilder sql = new StringBuilder();
sql.AppendLine("SELECT * FROM CUSTOMERS ");
sql.AppendLine("WHERE @CustomerId = null OR CustomerId = @CustomerId ");
sql.AppendLine("AND @CustomerName = null OR CustomerName = @CustomerName ");
var customerList = context.Customers.SqlQuery(sql.ToString(),
new SqlParameter("@CustomerId", customerId),
new SqlParameter("@CustomerName", customerName)).ToList();
Anche se per la tua condizione attuale puoi usare LINQ.
var customer = context.Customers.Where(c => c.CustomerId = id).Distinct();
Questo è il modo in cui esegui le query SQL raw di Entity Framework
using (var context = new DbContext())
{
context.Database.SqlCommand(
"UPDATE dbo.Blogs SET Name = 'Another Name' WHERE BlogId = 1");
}
using (var context = new DbContext())
{
var customers = context.Customers.SqlQuery("SELECT * FROM dbo.Customers").ToList();
}
using (var context = new DbContext())
{
var customers = context.Blogs.SqlQuery("dbo.GE_Customers").ToList();
}
using (var context = new DbContext())
{
var customerNames = context.Database.SqlQuery<string>(
"SELECT Name FROM dbo.Customers").ToList();
}
Aggiornamento per rispondere
Non è necessario passare SqlParameter, è sufficiente passare gli oggetti predefiniti
Penso che il codice qui sotto dovrebbe funzionare bene.
var customerList = context.Customers.SqlQuery(sql.ToString(), customerId, customerName).ToList();
se la tua vera domanda è
sql.AppendLine("SELECT * FROM CUSTOMERS ");
sql.AppendLine("WHERE @CustomerId = null OR CustomerId = @CustomerId ");
sql.AppendLine("AND @CustomerName = null OR CustomerName = @CustomerName ");
Ti suggerirei di farlo in questo modo
var customers = context.Costomers; // this does not populates the result yet
if (!String.IsNullOrEmpty(customerId))
{
customers = customers.Where(c => c.CustomerId = customerId); // this does not populates the result yet
}
if (!String.IsNullOrEmpty(customerName))
{
customers = customers.Where(c => c.CustomerName = customerName); // this does not populates the result yet
}
// finally execute the query
var custList = customers.ToList();
Se non ci sono limiti o requisiti, non utilizzare questo tipo di query sono vulnerabili agli attacchi SQL injection.
Puoi fare quasi ogni frase con Entity Framework usando linq, come quello che hai scritto
DbContext.Customer.Where(c => c.CustomerId = id).Distinct();
È più leggibile e più sicuro.