views:

371

answers:

1

My Linq To Sql query

PROJETS = PROJETS.Where(p => (p.VilleArrive != "" && p.VilleArrive != null) &&  p.VilleArrive.Contains(alerte.VilleArrive));

is translated like this

SELECT * // (many columns)
FROM [dbo].[cov_Projet] AS [t0]
WHERE ([t0].[VilleArrive] <> @p0)     // city != ""
AND ([t0].[VilleArrive] IS NOT NULL)  // city != null
AND ([t0].[VilleArrive] LIKE @p1)     // city.contains(alert.city)
ORDER BY [t0].[DateDebut]

It is well executed when i run it manually into sql server. But it returns a ArgumentNullException exception when executed by linq. Actually the column tested "VilleArrive" ("city") is never null but always an empty string

I really don't understand why it appends. My problem looks like this one http://stackoverflow.com/questions/977159/linq-to-sql-and-null-strings-how-do-i-use-contains but answers do not work with me.

The "LIKE" keyword seems to invoke the Linq.SqlClient.SqlHelpers.GetStringContainsPattern(String text, Char escape)

Thank you for your help , sorry for my english.

Here the stack trace my project is an ASP MVC 1.0 project

System.ArgumentNullException was unhandled by user code Message="La valeur ne peut pas être null.\r\nNom du paramètre : text"
Source="System.Data.Linq"
ParamName="text" StackTrace: à System.Data.Linq.SqlClient.SqlHelpers.GetStringContainsPattern(String text, Char escape) à System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.TranslateStringMethod(SqlMethodCall mc) à System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitMethodCall(SqlMethodCall mc) à System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) à System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp) à System.Data.Linq.SqlClient.SqlVisitor.VisitBinaryOperator(SqlBinary bo) à System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitBinaryOperator(SqlBinary bo) à System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) à System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp) à System.Data.Linq.SqlClient.SqlVisitor.VisitSelectCore(SqlSelect select) à System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitSelect(SqlSelect select) à System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) à System.Data.Linq.SqlClient.SqlVisitor.VisitAlias(SqlAlias a) à System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) à System.Data.Linq.SqlClient.SqlVisitor.VisitSource(SqlSource source) à System.Data.Linq.SqlClient.SqlVisitor.VisitSelectCore(SqlSelect select) à System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitSelect(SqlSelect select) à System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) à System.Data.Linq.SqlClient.SqlVisitor.VisitUnion(SqlUnion su) à System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) à System.Data.Linq.SqlClient.SqlVisitor.VisitAlias(SqlAlias a) à System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) à System.Data.Linq.SqlClient.SqlVisitor.VisitSource(SqlSource source) à System.Data.Linq.SqlClient.SqlVisitor.VisitSelectCore(SqlSelect select) à System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitSelect(SqlSelect select) à System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) à System.Data.Linq.SqlClient.SqlProvider.BuildQuery(ResultShape resultShape, Type resultType, SqlNode node, ReadOnlyCollection1 parentParameters, SqlNodeAnnotations annotations) à System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations) à System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.GetQueryText(Expression query) à System.Data.Linq.DataQuery1.ToString() à covCake.Services.ProjetAlerts.RetrieveProjectsByUsers(Boolean UpdateAlerts) dans D:\AspProject\covCake\covCake\Services\ProjetAlerts.cs:ligne 111 à covCake.Controllers.AlertesController.SendAlertEmail(String p) dans D:\AspProject\covCake\covCake\Controllers\AlertesController.cs:ligne 152 à lambda_method(ExecutionScope , ControllerBase , Object[] ) à System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) à System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) à System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) à System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassa.b__7() à System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException:

A: 

Try removing the != "" comparison, and make sure the parameter you are passing isn't null:

PROJETS = PROJETS.Where(p => (p.VilleArrive != null) &&
                             (alerte.VilleArrive != null) &&
                              p.VilleArrive.Contains(alerte.VilleArrive));
Yannick M.