مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

ebrahimi6357

عضویت از 1392/07/05

ایجاد کوئری برای مانده حساب

  • دوشنبه 1 شهریور 1395
  • 04:16
تشکر میکنم

سلام

دوستان من در دیتابیس دو جدول Accounts و Documents دارم که در اولی مشخصات حسابها و دومی مقادیر اسناد نگهداری میشوند

در حالت عادی با کوئری های زیر میشه در SQL Server  مانده حساب را به صورت زیر مشاهده کرد

SELECT        AccountID, SUM(DocumentValue) AS SamBes
FROM            dbo.Documents
WHERE        (DocumentTypeID = 1)
GROUP BY AccountID

SELECT        AccountID, SUM(DocumentValue) AS SamBed
FROM            dbo.Documents
WHERE        (DocumentTypeID = 2)
GROUP BY AccountID

SELECT        dbo.Accounts.AccountID, dbo.Accounts.AccountTypeID, dbo.Accounts.AccountMasterID, dbo.Accounts.AccountName, { fn IFNULL(dbo.BesAccount.SamBes, 0) } AS Bes, { fn IFNULL(dbo.BedAccount.SamBed, 0)
                         } AS Bed, { fn IFNULL(dbo.BesAccount.SamBes, 0) } - { fn IFNULL(dbo.BedAccount.SamBed, 0) } AS Mandeh
FROM            dbo.Accounts LEFT OUTER JOIN
                         dbo.BesAccount ON dbo.Accounts.AccountID = dbo.BesAccount.AccountID LEFT OUTER JOIN
                         dbo.BedAccount ON dbo.Accounts.AccountID = dbo.BedAccount.AccountID


 

اما وقتی همین کوئری ها به LINQ تبدیل میشه و میشه این کوئری ارور میده               

var QAccounts = from A in Con.Accounts
                                select A;
                var Bed = from D in Con.Documents
                          where
                            D.DocumentTypeID == 2
                          group D by new
                          {
                              D.AccountID

                          } into g
                          select new
                          {
                              g.Key.AccountID,
                              SamBed = g.Sum(p => p.DocumentValue)
                          };
                var Bes = from D in Con.Documents
                          where
                            D.DocumentTypeID == 1
                          group D by new
                          {
                              D.AccountID,
                              D.DocumentTypeID
                          } into g
                          select new
                          {
                              g.Key.AccountID,
                              SamBes = g.Sum(p => p.DocumentValue)
                          };
                var AccountDetals = from QA in QAccounts
                                    join QBes in Bes on new { AccountID = QA.AccountID } equals new { AccountID = QBes.AccountID }
                                    into BesAccount_join
                                    from QBes in BesAccount_join.DefaultIfEmpty()
                                    join QBed in Bed on new { AccountID = QA.AccountID } equals new { AccountID = QBed.AccountID } into BedAccount_join
                                    from QBed in BedAccount_join.DefaultIfEmpty()
                                    select new
                                    {
                                        QA.AccountID,
                                        QA.AccountTypeID,
                                        QA.AccountMasterID,
                                        QA.AccountName,
                                        SBes = (QBes.SamBes ?? 0),
                                        SBed = (QBed.SamBed ?? 0),
                                    };

کسی میدونه چطور باید نوشته بشه؟

پاسخ های این پرسش

تعداد پاسخ ها : 1 پاسخ
کاربر سایت

ebrahimi6357

عضویت از 1392/07/05

  • یکشنبه 7 شهریور 1395
  • 11:59

خیلی جالبه

ظاهرا تبلیغات برای کلاسها از پاسخگویی به سوالات خیلی مهتره تو این سایت

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)