LINQ中不区分大小写的命令

时间:2018-07-05 20:47:24

标签: c# linq

我正在尝试使用exist方法在LINQ中创建不区分大小写的搜索。这是代码,我在标记的行中收到错误。错误为“方法“不超载”包含2个参数”。有关如何操作的任何建议?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnderstandingLINQ
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Car> myCars = new List<Car>() {
                new Car() { VIN="A1", Make = "BMW", Model= "550i", StickerPrice=55000, Year=2009},
                new Car() { VIN="B2", Make="Toyota", Model="4Runner", StickerPrice=35000, Year=2010},
                new Car() { VIN="C3", Make="BMW", Model = "745li", StickerPrice=75000, Year=2008},
                new Car() { VIN="D4", Make="Ford", Model="Escape", StickerPrice=25000, Year=2008},
                new Car() { VIN="E5", Make="BMW", Model="55i", StickerPrice=57000, Year=2010}
            };

            Console.WriteLine("What term would you like to search?");
            string search = Console.ReadLine();
            myCars.Contains(search, StringComparer.OrdinalIgnoreCase));  //ERROR IS HERE
            Console.ReadLine();

        }
    }

    class Car
    {
        public string VIN { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
        public int Year { get; set; }
        public double StickerPrice { get; set; }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试下面的代码,使用使用FindAll的实现,并打印所有符合搜索条件的汽车。希望能帮助到你。 请在http://rextester.com/QNLS50990

找到一个没有Console.Readline的修改后的运行版本
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<Car> myCars = new List<Car>() {
                new Car() { VIN="A1", Make = "BMW", Model= "550i", StickerPrice=55000, Year=2009},
                new Car() { VIN="B2", Make="Toyota", Model="4Runner", StickerPrice=35000, Year=2010},
                new Car() { VIN="C3", Make="BMW", Model = "745li", StickerPrice=75000, Year=2008},
                new Car() { VIN="D4", Make="Ford", Model="Escape", StickerPrice=25000, Year=2008},
                new Car() { VIN="E5", Make="BMW", Model="55i", StickerPrice=57000, Year=2010}
            };

            Console.WriteLine("What term would you like to search?");
            // string search = Console.ReadLine();
            var search = "BM";

            var results = string.IsNullOrEmpty(search) ? 
                            null :
                            myCars.FindAll(car => 
                                  car.VIN.Contains(search, StringComparison.InvariantCultureIgnoreCase) ||
                                  car.Make.Contains(search, StringComparison.InvariantCultureIgnoreCase) ||
                                  car.Model.Contains(search, StringComparison.InvariantCultureIgnoreCase) ||
                                  car.StickerPrice.ToString().Contains(search, StringComparison.InvariantCultureIgnoreCase) ||
                                  car.Year.ToString().Contains(search, StringComparison.InvariantCultureIgnoreCase));

            if (results!= null && results.Count != 0)
            {
                Console.WriteLine();
                Console.WriteLine("Cars searched with " + search);
                results.ForEach(c => Console.Write("\n{0}\t{1}\t{2}\t{3}\t{4}", c.VIN, c.Make, c.Model, c.StickerPrice, c.Year));
            }
            else
            {
                Console.WriteLine("\nNo Cars found.");
            }
            //Console.ReadLine();
        }
    }

    public static class StringExtensions
    {
        public static bool Contains(this string sourceString, string toCheck, StringComparison comparison)
        {
            return sourceString != null ? sourceString.IndexOf(toCheck, comparison) >= 0 : false;
        }
    }

    public class Car
    {
        public string VIN { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
        public int Year { get; set; }
        public double StickerPrice { get; set; }
    }
}

答案 1 :(得分:0)

假设我理解您的追求,这是一个简单的方法(根据需要/必要进行改进):

//Given an input of "bmW" in whatever casing for "make"
var input = "bmW";

//myCars.Where(c => c.Make.ToLower() == input.ToLower()).ToList().ForEach(r => Console.WriteLine("VIN: {0} Model {1}, Price {2}, Year {3}", r.VIN, r.Model, r.StickerPrice, r.Year));

//was being too lazy, and based on @JohnWu's comment, interesting read and comments...:
myCars.Where(c => String.Equals(c.Make, input, StringComparison.OrdinalIgnoreCase)).ToList().ForEach(r => Console.WriteLine("VIN: {0} Model {1}, Price {2}, Year {3}", r.VIN, r.Model, r.StickerPrice, r.Year));

输出:

VIN: A1 Model 550i, Price 55000, Year 2009
VIN: C3 Model 745li, Price 75000, Year 2008
VIN: E5 Model 55i, Price 57000, Year 2010