public void HandleSearchProduct() { AppLogger.logInfo(this.GetType().Name, " ENTER HandleSearchProduct"); string barcode = _model.SearchBarcode ?? string.Empty; string name = _model.SearchName ?? string.Empty; var products = from p in _business.GetAll() where ( (string.IsNullOrEmpty(barcode) == false && p.Barcode.Contains(barcode)) || (string.IsNullOrEmpty(barcode) && p.Barcode.Equals(barcode)) || _model.IsSearchBarcode == false ) && ( (string.IsNullOrEmpty(name) == false && p.Name.ToLower().Contains(name.ToLower())) || (string.IsNullOrEmpty(name) && p.Name.ToLower().Equals(EntityFunctions.AsNonUnicode(name.ToLower()))) || _model.IsSearchName == false ) && ( string.IsNullOrEmpty(_model.SearchPriceFrom) || int.Parse(_model.SearchPriceFrom) <= p.Price || _model.IsSearchPriceFrom == false ) && ( string.IsNullOrEmpty(_model.SearchPriceTo) || int.Parse(_model.SearchPriceTo) >= p.Price || _model.IsSearchPriceTo == false ) && ( string.IsNullOrEmpty(_model.SearchStockFrom) || int.Parse(_model.SearchStockFrom) <= p.StockQuantity || _model.IsSearchStockFrom == false ) && ( string.IsNullOrEmpty(_model.SearchStockTo) || int.Parse(_model.SearchStockTo) >= p.StockQuantity || _model.IsSearchStockTo == false ) select p; _model.Products = products.ToList(); }
/// <summary> /// 用于 EF 查询语句 字符串条件筛选 /// 因为.Net字符串是Unicode格式 在数据库 执行时 会进行数据转换, /// 也就是说如果你在表中建立了索引此时会失效代替的是造成全表扫描 /// </summary> /// <param name="str"></param> /// <param name="EF"></param> /// <returns></returns> public static string AsNonUnicode(this string str) { int EF = 6; if (string.IsNullOrEmpty(str)) { return(string.Empty); } if (EF < 6) { return(EntityFunctions.AsNonUnicode(str)); } else { return(DbFunctions.AsNonUnicode(str)); } }
/// <summary> /// ! Test varchar vs nvarchar /// </summary> public static void QueryCustomersEmailFilter() { using (var context = new EFDbContext()) { // constant var q = context.Customers.Where(t => t.Email == "*****@*****.**").Select(t => t.FirstName); var result = q.ToList(); string email = "*****@*****.**"; q = context.Customers.Where(t => t.Email == email).Select(t => t.FirstName); result = q.ToList(); q = context.Customers.Where(t => t.Email == EntityFunctions.AsNonUnicode(email)).Select(t => t.FirstName); result = q.ToList(); } }