不区分大小写的lambda表达式

时间:2016-09-29 16:57:36

标签: c# wpf linq lambda

我正在使用Visual Studio 2015,Entity Framework 6,并尝试构建LINQ表达式以基于动态WHERE子句获取结果。用户可以选择搜索employeeIdsecurityId(字符串)或lastName。对于姓氏,它应该进行不区分大小写的搜索,因此用户可以输入大写或小写searchValue

以下是我所拥有的:

public async Task<ObservableCollection<EmployeeViewModel>> 
    SearchEmployeesAsync(string selectedColumn, string searchValue)
{   
    var paramEmployee = Expression.Parameter(typeof(Employee), "e");
    Func<EmployeeBase, bool> comparison = null;
    if (selectedColumn.Equals("employeeId"))
    {
        var employeeId = -1;
        int.TryParse(searchValue, out employeeId);
        comparison = Expression.Lambda<Func<Employee, bool>>(
            Expression.Equal(
                Expression.Property(paramEmployee, selectedColumn),
                Expression.Constant(employeeId)),
            paramEmployee).Compile();
    }
    else
    {
        comparison = Expression.Lambda<Func<Employee, bool>>(
            Expression.Equal(
                Expression.Property(paramEmployee, selectedColumn),
                Expression.Constant(searchValue)),
            paramEmployee).Compile();
    }

    using (var context = new MyEntities())
    {
        var query = (from e in context.Employees
            .Where(comparison)
            select new EmployeeViewModel
            {
                // Populate view model from entity object here
            });
        return await Task.Run(() => new ObservableCollection<EmployeeViewModel>(query));
    }
}

如何更改上述代码,使comparisonsecurityIdlastName上的搜索不区分大小写(两者都是数据库中的字符串)?上面securityId块涵盖lastNameelse。如果有更好的方法,我也愿意重构代码。我不想做的一件事是使用第三方库来编写动态WHERE子句。

谢谢。

2 个答案:

答案 0 :(得分:2)

如果您希望过滤应用于数据库而不是内存,那么在Expression<Func<Employee, bool>>子句中使用Where而不是Func<Employee, bool>中的ToLower至关重要码。不区分大小写的比较可以使用Task.Run方法进行模拟。

另外,正如其他人所说,最好使用ToListAsync中的System.Data.Linq.QueryableExtensions方法来消除using System.Data.Entity; public async Task<ObservableCollection<EmployeeViewModel>> SearchEmployeesAsync(string selectedColumn, string searchValue) { var parameter = Expression.Parameter(typeof(T), "e"); Expression left = Expression.PropertyOrField(parameter, selectedColumn); object value = searchValue; if (selectedColumn == "employeeId") { var employeeId = -1; int.TryParse(searchValue, out employeeId); value = employeeId; } else { // case insensitive left = Expression.Call(left, "ToLower", Type.EmptyTypes); value = searchValue.ToLower(); } var comparison = Expression.Lambda<Func<T, bool>>( Expression.Equal(left, Expression.Constant(value)), parameter); using (var context = new MyEntities()) { var query = context.Employees .Where(comparison) .Select(e => new EmployeeViewModel { // Populate view model from entity object here }); var result = await query.ToListAsync(); return new ObservableCollection<EmployeeViewModel>(result); } } 来电。

话虽如此,实施可能是这样的:

mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) 

if ($num_rows > 0){
echo "This email is already registered.";
}   

else{
$insert_query = "INSERT INTO hw2 (firstname, lastname, email, password, repassword) VALUES ('$firstname', '$lastname', '$email', '$password', '$repassword')";

echo "$insert_query";
mysqli_query($link,$insert_query)
}   

答案 1 :(得分:1)

我建议你这么简单。我对Task.Run提出质疑,但由于这不是问题的一部分,我就把它留下了。

[[NSArray alloc] initWithArray:arraytoBeCopied copyItems:YES];