我認爲@dotNET的答案似乎對我來說是正確的答案。爲了完整起見,我建立了一個示例,並且實施了不區分大小寫的搜索:
using System;
using System.Linq;
using Bogus;
public class Program
{
public static void Main()
{
var productsFactory = new Faker<Product>()
.StrictMode(true)
.RuleFor(p => p.TransactionDate, f => f.Date.Between(DateTime.UtcNow.AddDays(-100), DateTime.UtcNow))
.RuleFor(p => p.Category, f => f.Commerce.ProductAdjective())
.RuleFor(p => p.Price, f => f.Random.Decimal(10, 100))
.RuleFor(p => p.PaymentType, f => f.PickRandomWithout(PaymentType.Unknown))
.RuleFor(p => p.Name, f => f.Commerce.ProductName())
.RuleFor(p => p.City, f => f.Address.City())
.RuleFor(p => p.State, f => f.Address.State())
.RuleFor(p => p.Country, f => f.Address.Country());
var products = productsFactory.Generate(50);
var userSearch = products.Skip(3).First().Name.Substring(2, 3);
var productSearch = products.Where(product => product.Name.IndexOf(userSearch, StringComparison.CurrentCultureIgnoreCase) >= 0);
foreach (var result in productSearch)
{
Console.WriteLine(String.Format("{0:G} {1} {2:N2}", result.TransactionDate, result.Name, result.Price));
}
Console.ReadKey();
}
}
public class Product
{
public DateTime TransactionDate { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
public PaymentType PaymentType { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
public enum PaymentType
{
Unknown,
Mastercard,
Visa,
Amex,
}
什麼是「銷售」? 'product.ToString()'返回什麼?在方法開始時,「銷售」中是否還有任何元素? (在搜索失敗後'銷售'將被清空!) –
嘗試修整字符串 – Sajeetharan
這應該很容易調試。在查詢行放置一個斷點並檢查'userSearch'和'sales'的值。這可能與底層商店中存在空白字符有關。 – dotNET