Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
T-SQL query to LINQ
Message
 
 
To
20/10/2014 19:17:21
John Baird
Coatesville, Pennsylvania, United States
General information
Forum:
ASP.NET
Category:
LINQ
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01609660
Message ID:
01609691
Views:
48
>>Ok, this compiles:
>>
>>var invoicesList = from invoices in _dbSet.Where(x => x.Account == accountName && x.Hidden == false)
>>                               let balance = (from transact in _siriusContext.Transact where transact.InvoiceNo == invoices.InvoiceNo
>>                                                  select transact).Sum(c=>c.Extension)
>>                                select new InvoicesList
>>                                {
>>                                    InvoiceNo = invoices.InvoiceNo,
>>                                    Account = invoices.Account,
>>                                    Hidden = invoices.Hidden,
>>                                    Descrip = invoices.Descrip1,
>>                                    Created = invoices.DateTime,
>>                                    Finalized = invoices.Finalized,
>>                                    Balance = (decimal?)balance??0
>>                                };
>>
>>I'll try testing it now.
>
>why do you post a half-assed question, then change the parameters that you need later. I wasted some time figuring out a solution based on your original sample. Silly me, I should have known better..

I didn't change parameters. This is the exactly same query I was talking about from the start. This is my current solution:
 public IEnumerable<InvoicesList> GetByAccountName(string accountName, bool onlyInvoicesList = true,  bool showFinalized = false)
        {
            var invoicesList = _dbSet.Where(x => x.Account == accountName && x.Hidden == false).
                              Select(x => new InvoicesList { InvoiceNo = x.InvoiceNo, Account = x.Account, Hidden = x.Hidden});            

            if (!onlyInvoicesList)
            {
                 invoicesList = from invoices in  _dbSet.Where(x => x.Account == accountName && x.Hidden == false)

                               let balance = (from transact in _siriusContext.Transact
                                              where transact.InvoiceNo == invoices.InvoiceNo
                                              select transact).Sum(c => c.Extension)
                               select new InvoicesList
                               {
                                   InvoiceNo = invoices.InvoiceNo,
                                   Account = invoices.Account,
                                   Hidden = invoices.Hidden,
                                   Descrip = invoices.Descrip1,
                                   Created = invoices.DateTime,
                                   Finalized = invoices.Finalized,
                                   Balance = (decimal?)balance ?? 0
                               };
            }
            if (showFinalized)
            {
                invoicesList = invoicesList.Where(x => x.Finalized == true);
            }

            return invoicesList;
        }
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform