The <entity> search list for entries with the appropriate value for the class property?

I created a Model class in my ASP.Net Forms application called Years_Of_Service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PROJECT.Models.Default
{
    public class Years_Of_Service
    {
        public int Year { get; set; }
        public string FiscalYear
        {
            get
            {
                return Year + "-" + (Year + 1);
            }
        }
        public decimal ServiceCredited { get; set; }
        public decimal Salary { get; set; }
        public string CoveredEmployer { get; set; }


        public string TotalService { get; set; }
        public string PurchasedorReinstatedService { get; set; }
    }
}

I successfully process my database and return the corresponding values ​​in this model class through my user control, however in some cases I return records that have the same value [Year]:

Grid

Now I need to loop through each object in my list, and if 2 or more objects have an appropriate value for [Year], combine them together and change the value of [Private employer] to “Multiple employers”.

foreach , , - [] 2. - , ?

, :

public partial class YearsOfService : System.Web.UI.UserControl
{
    public List<Years_Of_Service> lyos { get; set; }
    public Years_Of_Service y_o_s { get; set; }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ApplyData();
        }
    }

    private void ApplyData()
    {

        foreach (Years_Of_Service yearsOfService in lyos)
        {
            // Search [lyos] for records with same [Year] value (ex. "2009-2010")
            // If found, Add the [Service] & [Salary] Values together under one [Year] record.
            // Change the newly "merged" record to say "Multiple Employers" for [CoveredEmployer]
        }

        salaryGridView.DataSource = lyos;
        salaryGridView.DataBind();

        if (y_o_s.TotalService.ToString() != null)
        {
            creditSalLabel.Text = y_o_s.TotalService.ToString();
        }
        else
        {
            creditSalLabel.Text = String.Empty;
        }

        if (y_o_s.PurchasedorReinstatedService.ToString() != null)
        {
            purchaseCreditLabel.Text = y_o_s.PurchasedorReinstatedService.ToString();
        }
        else
        {
            purchaseCreditLabel.Text = String.Empty;
        }
    }
}
+4
1

.

:

var groupedByYearLyos = lyos.GroupBy(x => x.Year)
    .Select(x => new Years_Of_Service() 
        { 
            CoveredEmployer = x.Count() > 1 ? "Multiple Employers" : x.First().CoveredEmployer,
            Year = x.Key,
            Salary = x.Sum(y => y.Salary)
             //and etc 
        })
    .ToList();
+4

All Articles