简体   繁体   中英

Displaying list of values from a list view into body of an email

I am trying to display a list of pickup numbers in a body of an email. I can get the pickup number fine using this code:

foreach (LoadRelationship relationship in View.SelectedObjects)
{
///This works fine, I get 3 pickup numbers for my selected records///
    List<string> pickUpNumbers = View.SelectedObjects.Cast<LoadRelationship>()
    .Where(x => x != null && x.PurchaseLoad.PickupNumber != null)
    .Select(x => x.PurchaseLoad.PickupNumber)
    .ToList();
//When assigning the pickUpNumbers.To String to the body of the email it fails here:
e.Report.ExportOptions.Email.Body =  pickUpNumbers.ToString();
}

The output in the body of the email is this:

System.Collections.Generic.List`1[System.String]

The out put should be this:

12345 2334556 12334445

尝试这个:

e.Report.ExportOptions.Email.Body = string.Join(" ", pickUpNumbers);

Try:

foreach (LoadRelationship relationship in View.SelectedObjects)
{
///This works fine, I get 3 pickup numbers for my selected records///
    List<string> pickUpNumbers = View.SelectedObjects.Cast<LoadRelationship>()
    .Where(x => x != null && x.PurchaseLoad.PickupNumber != null)
    .Select(x => x.PurchaseLoad.PickupNumber)
    .ToList();
//When assigning the pickUpNumbers.To String to the body of the email it fails here:

string body = "";
for(int i = 0; i < pickUpNumbers.Count; i++) {
   if (i > 0) {
       body += ", ";
   }
   body += puckUpNumbers[i];
}
e.Report.ExportOptions.Email.Body =  body;


}

The output is System.Collections.Generic.List1[System.String] because you have a collection of strings and are calling .ToString() on the collection directly.

You can get the result you want by iterating over the collection and build the e-mail body up.

For instance:

 StringBuilder sb = new StringBuilder();
 foreach(var pickupNumber in pickUpNumbers) {
     sb.Append(pickupNumber);
 }

 e.Report.ExportOptions.Email.Body = sb.ToString();

You can also use String.Join .

 e.Report.ExportOptions.Email.Body = string.Join(" ", pickUpNumbers);

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