>>>>>>>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:
>>>>>>>
>>>>>>> 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.
>>>>>>>
>>>>>>>Many thanks for your help.
>>>>>>
>>>>>>Don't see an easy way of avoiding the duplication but the code should be fine. But if TransactionHeader can have NO RSTransactions you will get an exception (ditto if no EMTransactions)
>>>>>
>>>>>Nono, it works fine. Coalescence ?? "" ?.
>>>>
>>>>That would work if (for example) RSTransactionType.Description was null. Won't if there is no first RSTransaction (i.e. if FirstOrDefault() returns null)
>>>
>>>Well ... Could it be that EF takes care of that ? I don't have a clue, but it does work.
>>
>>Doesn't take care of if for me :-{
>>If FirstOrDefault() returns null than trying to access any property of the expected type throws a NullReferenceException
>
>Strange no? The Count is zero, but the linq does not break. Maybe there's a DefaultIsEmpty that is set to the type somewhere up the line? But then you would expect count() to return 1 no? TransactionHeaders is of DbSet type. Puzzling, but should I worry? :)
Yup. Strange :-}
TBH I've not used 'joins' much in Linq to EF - much prefer specifying associations. But I thought DefaultIfEmpty() is normally only used with left outer joins (and even then must be included in the code).
But if it works and your happy - hey......