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 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 = t.TransactionHeader.RSTransactions.FirstOrDefault().RSTransactionType.Description ?? "", RS_Document = t.TransactionHeader.RSTransactions.FirstOrDefault().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); } } }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.