Ben tam kod var:
Bu Araç için örnek sınıfı olacak;
public static List<Car> myList = new List<Car>();
static void Main(string[] args)
{
myList.Add(new Car() { Model = "A", Name = "XXX", Brand = "Some Brand" });
myList.Add(new Car() { Model = "B", Name = "YYY", Brand = "Some Brand1" });
myList.Add(new Car() { Model = "C", Name = "ZZZ", Brand = "Some Brand2" });
Car search = new Car();
Console.Write("Search for model:");
search.Model = Console.ReadLine();
Console.WriteLine("Following Result Found for {0}", search.Model);
SearchLog(search);
}
Son olarak SearchLog
için imzadır: Burada
class Car
{
public string Model { get; set; }
public string Name { get; set; }
public string Brand { get; set; }
//Rest of properties here
public override string ToString()
{
string output = String.Format("Model :{0} \n Name :{1} \n Brand :{2}", this.Model, this.Name, this.Brand);
return output;
}
}
işlemleri yapmak ana işlevi olan
public static void SearchLog(Car search)
{
var resultList = myList.Where(x => x.Model == search.Model).ToList();
int i = 1;
foreach (var car in resultList)
{
Console.WriteLine("Result {0} : {1}", i++, myList[i].ToString());
}
}
başka önerim var; Arama, Car
sınıfının bir nesnesi olmamalıdır. Bunun yerine bir dize olabilir;
Aşağıdaki gibi kendi yolunu deneyebilirsiniz:
Console.Write("Search for model:");
string inputSearch = Console.ReadLine();
bool carFound = false;
for (int i = 0; i < myList.Count; i++)
{
if (myList[i].Model == inputSearch)
{
Console.WriteLine("Model: " + myList[i].Model);
carFound = true;
}
}
if (!carFound) { Console.WriteLine("None model were found"); }
kodunuzu görmek – Jace
Evet, ihtiyacımız size Araba sınıf kodunu paylaşın. MyList nerede ilan edildi? 'Search.Model' nerede kullanılıyor? –
Bu konuma hatanın neden olduğundan şüpheleniyorum, 'Console.WriteLine (" Model: {0} ", search.Model);' –