简体   繁体   中英

using foreach for C# for Email in Asp.NetCore Web API

I have two different IEnumerable<> one to get id from gmail inbox and the other IEnumerable<> is to get list of MailMessage like this code below

          client.DefaultMailbox = "INBOX";
          IEnumerable<uint> sids = client.Search(SearchCondition.Unseen());
          IEnumerable<MailMessage> AllMessage = client.GetMessages(sids);
          List<LiteInbox> allmail = new List<LiteInbox>();

Now i want to use two foreach to get particular Id for MailMessage coming from gmail inbox below if i iterate foreach inside foreach that will fecth the each id for each mail

 foreach (uint ids in sids)
  {
      id = ids;
      LiteInbox lite=new LiteInbox();

      foreach(var mail in AllMessage )
      {
         lite.id=id;
         lite.sender=mail.sender;
         lite.subject=mail.subject;
      }

   }

But when i run my code it first iterate the first foreach for all email before it then iterate the second foreach meaning if there is 4 unRead mail in my gmail, it will bring 16 unread emails and iterate each 4 with a single Id like this

     {   [
   {
    id: 1,
    subject: welcome,
    body:hbjdbjddjh,
    sender: wealsegun@gmail.com     },
{
    id: 1,
    subject: welcome,
    body:hbjdbjddjh,
    sender: wealsegun@gmail.com      },
{
    id: 1,
    subject: welcome,
    body:hbjdbjddjh,
    sender: wealsegun@gmail.com      },
{
    id: 1,
    subject: welcome,
    body:hbjdbjddjh,
    sender: wealsegun@gmail.com      },
],
   [
   {
    id: 2,
    subject: welcome,
    body:hbjdbjddjh,
    sender: wealsegun@gmail.com      },
{
    id: 2,
    subject: welcome,
    body:hbjdbjddjh,
    sender: wealsegun@gmail.com      },
{
    id: 2,
    subject: welcome,
    body:hbjdbjddjh,
    sender: wealsegun@gmail.com      },
{
    id: 2,
    subject: welcome,
    body:hbjdbjddjh,
    sender: wealsegun@gmail.com     },
]}

You need to retrieve every sid associated to every Email if the class MailMessage has a sid property (I doubt, is MailMessage class the .Net framework one?) you need to modify the signature of one or both your Search and GetMessages so it returns something like a

List<Pair<uint, MailMessage>> 

where every element of the list is the message and its ID

After that, you will not need the double foreach

Assuming that the order of AllMessage and sids is the same, and the number of elements in these lists is equal (otherwise I wouldn't know how to assign the correct id to a message) you can do the following

for (int i = 0; i < AllMessage.Count; i++) {
    LiteInbox lite=new LiteInbox();
    lite.id = sids[i];
    lite.sender=AllMessage[i].sender;
    lite.subject=AllMessage[i].subject;
    allmail.Add(lite);    
}

alternatively with foreach

int i = 0;  //you'll need that index to access the corresponding element in the other list
foreach (int id in sids) {
    LiteInbox lite=new LiteInbox();
    lite.id = id;
    lite.sender=AllMessage[i].sender;
    lite.subject=AllMessage[i].subject;
    allmail.Add(lite);            
    i++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM