Samantha Iren Martin Samantha Iren Martin - 26 days ago 8
C# Question

Appending results from LINQS query to paged list

I'm sure a simple fix but I can't seem to find a fix for the issue I'm having. The idea is that it would loop through the "extensions" and find the results for them all and I need to append the results at the end but everything I have tried doesn't seem to work. Any ideas would be greatly appreciated.

string s = cdr.callingPartyNumber;
string[] exts = s.Trim().Split(',');
foreach (string ext in exts)
{

var calls = db.CallDetails.Where(x => x.dateTimeConnect >= cdr.dateTimeConnect && x.dateTimeDisconnect <= cdr.dateTimeDisconnect && x.callingPartyNumber.Contains(ext)).Select(x =>
new CallDetailDTO
{
origSpan = x.origSpan,
callingPartyNumber = x.callingPartyNumber,
callingPartyUnicodeLoginUserID = x.callingPartyUnicodeLoginUserID,
originalCalledPartyNumber = x.originalCalledPartyNumber,
dateTimeConnect = x.dateTimeConnect,
duration = x.duration
})
.ToList();


cdr.TotalResults = cdr.TotalResults + calls.Count;
cdr.Calls = calls.ToPagedList(cdr.Page, pageSize);
}

Answer

The reason you only see the last calls is because you keep overriding cdr.Calls in each iteration:

cdr.Calls = calls.ToPagedList(cdr.Page, pageSize);

Im assuming cdr.Calls is some sort of List<CallDetailDTO>.

What you need to do is make it a List<List<CallDetailDTO>> and use List.Add for each ext:

cdr.Calls.Add(calls.ToPagedList(cdr.Page, pageSize));