본문 바로가기
코딩연습/C#

[C#]LINQ연습문제(레코드조회)

by 호아니 2020. 6. 12.

다음과 같은 배열에서 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}");

             
        }
    }
}

'코딩연습 > C#' 카테고리의 다른 글

[C#]람다식(Action대리자  (0) 2020.06.15
[C#]람다식(Func대리자)  (0) 2020.06.15
[C#]LINQ  (0) 2020.06.12
[C#]대리자의 이벤트  (0) 2020.06.12
[C#]대리자 체인  (0) 2020.06.12