سلام
دوستان من در دیتابیس دو جدول 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), };
کسی میدونه چطور باید نوشته بشه؟
خیلی جالبه
ظاهرا تبلیغات برای کلاسها از پاسخگویی به سوالات خیلی مهتره تو این سایت
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)