Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
XML Anonymous Types
Message
De
28/03/2017 05:30:24
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
Information générale
Forum:
ASP.NET
Catégorie:
XML
Versions des environnements
Environment:
C# 5.0
OS:
Windows 10
Network:
Windows Server 2016
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01649231
Message ID:
01649425
Vues:
56
This message has been marked as the solution to the initial question of the thread.
>Thanks. I found that using Reflection I was able to have my ExcelWrapper SaveLinq Of T. Turned out easier than I thought.
>
>Slightly off topic and as Cetin pointed out to me, I should make use of the relationships so my Linq now looks like this:
> ...
>This now works fine. I am a little bit worried about this though:
>
>
>                            RS_Type = t.TransactionHeader.RSTransactions.FirstOrDefault().RSTransactionType.Description ?? "",
>                             RS_Document = t.TransactionHeader.RSTransactions.FirstOrDefault().Document ?? "",
>
>
>There is a 1 to M relation between TransactionHeaders and RSTransactions. But I'm really only interested in the first record, but I need two fields (Description and Document). The fact that I issue twice FirstOrDefault does not look right.
>
>Many thanks for your help.

I generally prefer method syntax, but ability to use "let" keyword is one place where you can't use method syntax and need to resort to comprehension syntax. Since here comprehension syntax is used it would fit in nicely:
        public static void GetTransactions()
        {
            using (var context = new PICSEntities(Config.model.SqlServer))
            {
                var q = (from p in context.Parcels
                         join t in context.TransactionDetails on p.Parcels_Id equals t.Parcels_Id
                         let firstTransaction = t.TransactionHeader.RSTransactions.FirstOrDefault() 
                         select new
                         {
                             Parcels_Id = p.Parcels_Id,
                             Goods = p.GoodsType.Description,
                             Inventory = p.ParcelInventoryType.Description,
                             RSReference = p.RSReference,
                             Transaction_Nr = t.TransactionHeaders_Id,
                             Transaction = t.TransactionHeader.TransactionType.Description,
                             Date = t.TransactionHeader.TransactionDate,
                             Weight = t.Weight,
                             Amount = t.Amount,
                             EM_Document = t.TransactionHeader.EMTransactions.FirstOrDefault().Document ?? "",
                             EM_Count = t.TransactionHeader.EMTransactions.Count(),
                             RS_Type = firstTransaction.RSTransactionType.Description ?? "",
                             RS_Document = firstTransaction.Document ?? "",
                             RS_Count = t.TransactionHeader.RSTransactions.Count()
                         }).ToList();               
                using (var xl = new ExcelWrapper())
                {
                    xl.wb = xl.wbs.Add();
                    xl.ws = xl.wb.ActiveSheet;
                    string filename = SequencedFilename.get(@"c:\docs\Parcels", $".xlsx");
                    xl.SaveLinq(q, filename);
                }
            }
        }
As per the ExcelWrapper, I think you wouldn't need reflection, if you use Epplus library from Nuget, you could save any collection to Excel (with or without excel being installed). It is not very fast as it claims but not slow either. Here is a sample:
ExcelPackage pck = new ExcelPackage();

var wsEnum = pck.Workbook.Worksheets.Add("MyData");
wsEnum.Cells["A1"].LoadFromCollection(myCollection, true, TableStyles.Medium9);
wsEnum.Cells[wsEnum.Dimension.Address].AutoFitColumns();

var fi = new FileInfo(@"c:\temp\ExcelSample.xlsx");
if (fi.Exists)
{
  fi.Delete();
}
pck.SaveAs(fi);
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform