0

GridView にこの linq クエリがあります

 var table = from ll in dbo.Workers
                    join p in dbo.WorkDays on ll.Id equals p.Id
                    orderby p.Enter
                    select new
                    {
                        ll.Id,
                        ll.Name,
                        ll.Salary,
                        p.Enter,
                        p.ExitT,
                        p.Place,
                        WorkTime = Math.Round(getWorkTime(p.Enter, p.ExitT), 2),
                        Earned = Math.Round(getEarned(p.Enter, p.ExitT, ll.Salary), 2),
                    };

テーブルに合計の列を追加するにはどうすればよいですか?

4

1 に答える 1

1

データを保持し、そのデータに基づいて計算を提供するクラスを作成します。

public class WorkerInfo
{
    // I don't know exact type of fields, but its enough for you to get the idea
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Salary { get; set; }
    public DateTime Enter { get; set; }
    public DateTime ExitT { get; set; }
    public string Place { get; set; }

    public int WorkTime
    {
       get { return (ExitT - Enter).TotalDays; }
    }

    public decimal Earned
    {
       get { return WorkTime * Salary; }
    }
}

これらのエンティティにデータを入力します。

var workers = from ll in dbo.Workers
              join p in dbo.WorkDays on ll.Id equals p.Id
              orderby p.Enter
              select new WorkerInfo
              {
                 Id = ll.Id,
                 Name = ll.Name,
                 Salary = ll.Salary,
                 Enter = p.Enter,
                 ExitT = p.ExitT,
                 Place = p.Place
              }; 

すべての日の集計データを返すためのUPDATEサンプル:

var query = from w in dbo.Workers
            join d in dbo.WorkDays on w.Id equals d.Id into days
            let WorkTime = days.Sum(d => d.ExitT - d.Enter)
            select new 
              {
                 w.Id,
                 w.Name,
                 w.Salary,
                 WorkTime,
                 Earned = WorkTime * w.Salary
              }; 
于 2012-11-17T22:24:58.413 に答える