코딩연습/C#
[C#]LINQ연습문제(레코드조회)
호아니
2020. 6. 12. 14:38
다음과 같은 배열에서 Cost는 50 이상,MaxSpeed는 150이상인 레코드만 조회하는 LINQ를 작성
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp13
{
class Program
{
class Car //프로필 클래스
{
public int Cost { get; set; } //객체 생성
public int MaxSpeed { get; set; }
}
static void Main(string[] args)
{
Car[] cars =
{
new Car(){Cost=56,MaxSpeed=120},
new Car(){Cost=70,MaxSpeed=150},
new Car(){Cost=45,MaxSpeed=180},
new Car(){Cost=32,MaxSpeed=200},
new Car(){Cost=82,MaxSpeed=280}
};
var selected = from Car in cars
where Car.Cost >= 50 && Car.MaxSpeed >= 150 //조건
select Car;
foreach (var c in selected)
Console.WriteLine($"{c.Cost},{c.MaxSpeed}");
}
}
}