첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ Product.cs

namespace TestProject;

/// <summary>
/// 제품
/// </summary>
public class Product
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 제품 코드 - ProductCode

    /// <summary>
    /// 제품 코드
    /// </summary>
    public string ProductCode { get; set; }

    #endregion
    #region 명칭 - Name

    /// <summary>
    /// 명칭
    /// </summary>
    public string Name { get; set; }

    #endregion
    #region 가격 - Price

    /// <summary>
    /// 가격
    /// </summary>
    public decimal Price { get; set; }

    #endregion
}

 

728x90

 

▶ ProductSummary.cs

namespace TestProject;

/// <summary>
/// 제품 요약
/// </summary>
public class ProductSummary
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 제품명 - ProductName

    /// <summary>
    /// 제품명
    /// </summary>
    public string ProductName { get; set; }

    #endregion
    #region 금액 - Amount

    /// <summary>
    /// 금액
    /// </summary>
    public string Amount { get; set; }

    #endregion
    #region 수량 - Quantity

    /// <summary>
    /// 수량
    /// </summary>
    public string Quantity { get; set; }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 문자열 구하기 - ToString()

    /// <summary>
    /// 문자열 구하기
    /// </summary>
    /// <returns>문자열</returns>
    public override string ToString()
    {
        return $"{ProductName} | {Amount} | {Quantity}";
    }

    #endregion
}

 

300x250

 

▶ Program.cs

namespace TestProject;

/// <summary>
/// 프로그램
/// </summary>
class Program
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Private

    #region 프로그램 시작하기 - Main()

    /// <summary>
    /// 프로그램 시작하기
    /// </summary>
    private static void Main()
    {
        Random random = new Random();

        List<Product> sourceList = new List<Product>();

        for(int i = 0; i < 100; i++)
        {
            int value = random.Next(1, 10);

            sourceList.Add(new Product { ProductCode = $"제품코드 {value}", Name = $"제품명 {value}", Price = random.Next(1000, 10000) });
        }

        List<ProductSummary> targetList = sourceList
            .GroupBy(source => source.ProductCode)
            .Select
            (
                sourceGroup => new ProductSummary
                {
                    ProductName = sourceGroup.First().Name,
                    Quantity    = sourceGroup.Count().ToString(),
                    Amount      = sourceGroup.Sum(c => c.Price).ToString(),
                }
            )
            .OrderBy(source => source.ProductName)
            .ToList();

        foreach(ProductSummary summary in targetList)
        {
            Console.WriteLine(summary);
        }
    }

    #endregion
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요