var contacts = (from c in _db.Contacts join dgm in _db.DistributionGroupMembers on c.Id equals dgm.ContactId join dg in _db.DistributionGroups on dgm.DistributionGroupId equals dg.Id join dgis in _db.DistributionGroupInSms on dg.Id equals dgis.DistributionGroupId where dgis.SmsId == SmsId select new { id = c.Id, FirstName = c.FirstName, LastName = c.LastName, PhoneNumber = c.PhoneNumber, Result = "Waiting to be sent" }).Distinct().ToArrayAsync(); foreach (var contact in contacts) //contacts is underlined { recipients.Recipients.Add(new RecipientsInSmsData() { Id = contact.id, FirstName = contact.FirstName, LastName = contact.LastName, PhoneNumber = contact.PhoneNumber, SendingResult = contact.SendingResult }); }
Compiler error message:
foreach statement cannot operate on variables of type 'Task<[]>' because 'Task<[]>' does not contain a public instance definition for 'GetEnumerator'
between the <>'s after task it says:
anonymous type: int id, string FirstName, string LastName, string PhoneNumber, string Result
答案 0 :(得分:2)
Asynchronous operations need to be await
ed in order to get the result, the documentation is a great starting point for learning how to use async
and await
.
The other problem here is that the foreach
is unnecessary if LINQ is used properly:
var contacts = await (
from c in _db.Contacts
join dgm in _db.DistributionGroupMembers on c.Id equals dgm.ContactId
join dg in _db.DistributionGroups on dgm.DistributionGroupId equals dg.Id
join dgis in _db.DistributionGroupInSms on dg.Id equals dgis.DistributionGroupId
where dgis.SmsId == SmsId
select new RecipientsInSmsData
{
Id = c.Id,
FirstName = c.FirstName,
LastName = c.LastName,
PhoneNumber = c.PhoneNumber,
Result = "Waiting to be sent"
})
.Distinct()
.ToArrayAsync();
recipients.Recipients.AddRange(contacts);