Asp.net mvc 4 MVC SQL无效

Asp.net mvc 4 MVC SQL无效,asp.net-mvc-4,Asp.net Mvc 4,我的MVC索引函数中有以下代码。从允许用户搜索各种字符串值的搜索页面调用: public ViewResult Index(string sortOrder, string YearString,string MonthString,string BudgetTypeString,string DescriptionString) { ViewBag.BudgetTypeSortParm = String.IsNullOrEmpty(sortOrder) ? "BudgetTyp

我的MVC索引函数中有以下代码。从允许用户搜索各种字符串值的搜索页面调用:

public ViewResult Index(string sortOrder, string YearString,string MonthString,string BudgetTypeString,string DescriptionString)
{
        ViewBag.BudgetTypeSortParm = String.IsNullOrEmpty(sortOrder) ? "BudgetType_desc" : "";
        ViewBag.MonthSortParm = sortOrder == "Date" ? "Month_desc" : "Month";
        ViewBag.YearSortParm = sortOrder == "Date" ? "Year_desc" : "Year"; // 12-24-2014 JR added
        ViewBag.DescriptionSortParm = sortOrder == "Description" ? "Description_desc" : "Description";
        var budg = from s in db.budgets
                   select s; 
        if (!String.IsNullOrEmpty(YearString) || 
            !String.IsNullOrEmpty(MonthString) || 
            !String.IsNullOrEmpty(BudgetTypeString) || 
            !String.IsNullOrEmpty(DescriptionString))
        {
            budg = budg.Where(s => s.BudgetType.ToUpper().Contains(BudgetTypeString.ToUpper()) || 
                String.IsNullOrEmpty(BudgetTypeString) || s.Description.ToUpper().Contains(DescriptionString.ToUpper()) || 
                String.IsNullOrEmpty(DescriptionString) || 
                s.Month.ToUpper().Contains(MonthString.ToUpper()) || 
                String.IsNullOrEmpty(MonthString) || 
                s.Year.ToUpper().Contains(YearString.ToUpper()) ||
                String.IsNullOrEmpty(YearString));
            budg = budg.OrderBy(s => s.BudgetType);
           return View(db.budgets.ToList());
       }

}                
以下是根据上述代码转换的实际SQL:

budg    {SELECT 
[Extent1].[id] AS [id], 
[Extent1].[BudgetType] AS [BudgetType], 
[Extent1].[Description] AS [Description], 
[Extent1].[Amount] AS [Amount], 
[Extent1].[Month] AS [Month], 
[Extent1].[Year] AS [Year], 
[Extent1].[DateStamp] AS [DateStamp]
FROM [dbo].[budget] AS [Extent1]
WHERE (( CAST(CHARINDEX(UPPER(@p__linq__0), 
UPPER([Extent1].[BudgetType])) AS int)) > 0) OR 
(( CAST(CHARINDEX(UPPER(@p__linq__1), UPPER([Extent1].[Description])) AS int)) > 0) OR
(( CAST(CHARINDEX(UPPER(@p__linq__2), UPPER([Extent1].[Month])) AS int)) > 0) OR
(( CAST(CHARINDEX(UPPER(@p__linq__3), UPPER([Extent1].[Year])) AS int)) > 0)}   

有人知道为什么我的字符串被错误地转换为整数,以及如何更正它,以便我的搜索页面上的搜索字符串正常工作吗?

让我们只看一个Cast语句:

( CAST(CHARINDEX(UPPER(@p__linq__1), UPPER([Extent1].[Description])) AS int)) > 0)
看一看内部陈述。它实际上是在另一个字符串中得到一个字符串的CharIndex。基本上,描述是否包含@p__linq____1。如果在另一个表达式中找不到第一个表达式,则CharIndex返回0。CharIndex的结果是被转换为int的值,然后比较它是否大于0。这正是您在使用.Contains时要求它执行的操作