0

次のようなクラスがあるとします。

    public class ServiceCall 
    { 
        public int ServiceCallID {get; set;}
        public DateTime ReportedTimestamp {get; set;}
        public bool IsPaid {get; set;}
        public decimal LabourNet { get; set;}
        public decimal LabourVat {get; set;}
    }
    public class UsedPart
    {
        public int UsedPartID { get; set; }
        public decimal NetPrice { get; set; }
        public decimal VatAmount {get; set;}
    }

次の形式でデータを返したい:

Month    #CallsReceived   AmountInvoiced    MoneyReceived
Jan 2009 202              €32050.20         €29200.00
Feb 2009 213              €35050.20         €34200.00

請求額は、1 か月のすべての (NetPrices + VatAmounts) + (LabourNet + LabourVat) net a の合計であり、受け取った金額は、IsPaid = true の場合のすべての合計です。このレポートの linq クエリを正しく取得できません。グループ化を行う場所と、Sum を実行するタイミングがわかりません。

現時点での linq クエリは次のとおりですが、現時点で必要なものに近いものは得られません。リンキーの良さを持っている人はいますか?

var q = from s in ServiceCalls
 join up in UsedParts on s.ServiceCallID equals up.ServiceCallID into tmp
 from nullablePart in tmp.DefaultIfEmpty()
 group s by s.ReportedTimestamp.Month
 into grp
 select new 
  { 
   Month = grp.Key,
   CallsReceived = grp.Count(),
   AmountInvoiced = grp.Sum(s => s.UsedParts.Sum(p => p.NetPrice)),
  };
4

2 に答える 2

0

OK、これを機能させるには、これをいくつかのサブクエリに分割する必要がありました。これを行うためのより優れた効率的な方法があるかもしれませんが、私は今のところlinqソリューションを持っています。IsPaid = true でフィルタリングすることを除いて、他のコードと非常に似ているため、Money Received を取得するためのコードは含めません。

LLBL Gen Pro を使用しているため、私の環境では少し異なりますが、次のコードから基本的な要点を得ることができます。

// Get the number of calls by month
var callsCount = from s in ServiceCalls group s by new DateTime(s.ReportedTimestamp.Year, s.ReportedTimestamp.Month, 1) into grp select new { ReportDate = grp.Key, Count = grp.Count()};
//callsCount.Dump();

// Get the labour on all calls by month
var serviceCallLabour = from s in ServiceCalls 
    select new 
    { 
        ReportDate = new DateTime(s.ReportedTimestamp.Year, s.ReportedTimestamp.Month, 1), 
        IsPaid = s.IsPaid, 
        GrossLabour = s.LabourChargeNet + s.LabourChargeVat 
    } 
    into labour
    group labour by labour.ReportDate into grp
    select new  {ReportDate = grp.Key, IsPaid = grp.Select(l => l.IsPaid).First(), GrossLabour = grp.Sum(l => l.GrossLabour) };
//serviceCallLabour.Dump();

// Get the value of parts used on all calls by month
var serviceCallParts = from s in ServiceCalls 
    join up in UsedParts on s.ServiceCallID equals up.ServiceCallID into tmp
    from np in tmp.DefaultIfEmpty()
    select new 
    { 
        ReportDate = new DateTime(s.ReportedTimestamp.Year, s.ReportedTimestamp.Month, 1),
        GrossPart = np != null ? (np.NetPrice + np.VatAmount) : 0
    }
    into callParts
    group callParts by callParts.ReportDate into grp
    select new { ReportDate = grp.Key, GrossPart = grp.Sum(p => p.GrossPart) };
//serviceCallParts.Dump();

var results = from l in serviceCallLabour
                join p in serviceCallParts on l.ReportDate equals p.ReportDate
                join c in callsCount on l.ReportDate equals c.ReportDate                
                select new { ReportDate = l.ReportDate, CallsReceived = c.Count, AmountInvoiced = l.GrossLabour + p.GrossPart} into callAmounts
                group callAmounts by callAmounts.ReportDate into grp
                select new {Month = grp.Key, CallsReceived = grp.Select(c => c.CallsReceived).First(), AmountInvoiced = grp.Sum(c => c.AmountInvoiced)};

results.Dump();
于 2009-11-05T15:53:03.520 に答える
0

この質問が古い歴史であることはわかっていますが、これが私の答えです。

var byMonth =
    from sc in ServiceCalls
    join up in UsedParts
        on sc.ServiceCallID equals up.ServiceCallID
        into gups
    let tsd = sc.ReportedTimestamp.Date
    let month = tsd.AddDays(1 - tsd.Day)
    group new
    {
        Calls = 1,
        sc.IsPaid,
        ChargeNet = sc.LabourNet + gups.Sum(gup => gup.NetPrice),
        ChargeVat = sc.LabourVat + gups.Sum(gup => gup.VatAmount),
    } by month into gscs
    select new
    {
        Month = gscs.Key,
        CallsReceived = gscs.Sum(gsc => gsc.Calls),
        AmountInvoiced
            = gscs.Sum(gsc => gsc.ChargeNet)
            + gscs.Sum(gsc => gsc.ChargeVat),
        MoneyReceived
            = gscs.Sum(gsc => gsc.IsPaid ? gsc.ChargeNet : 0m)
            + gscs.Sum(gsc => gsc.IsPaid ? gsc.ChargeVat : 0m),
    };

楽しみ!

于 2011-06-27T02:22:00.107 に答える