How do loop through an entity and print it out? (asp.net c#)

How would i loop though each invoice item and print it out to my email body with a <br/> after each item?  

Got stuck here.
       MyEntities2 m = new MyEntities2();
       InvoiceItem mi = m.InvoiceItems.ToList().Where(x => x.InvoiceNumber == v).Last();
      
        foreach(invice item in mi)
        {

        }

Open in new window


            msg.From = address;
            msg.Subject = "My Products";
            msg.Body = "loop each item here";

Open in new window

bmanmike39Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi bmanmike39;

I have modified your code to do what you need. I also modified your query to be more efficient. You do not want to bring down all the records from the source just to filter it down to one record.

MyEntities2 m = new MyEntities2();
InvoiceItem mi = m.InvoiceItems.Where(x => x.InvoiceNumber == v).Last();

string body = "";
foreach(invice item in mi)
{
    body += item.ToString() + "<br/>";
}

msg.From = address;
msg.Subject = "My Products";
msg.Body = body;

Open in new window

0
bmanmike39Author Commented:
I get a red squiggly line error "foreach statement can not operate on variable type InvoiceItem" because does not contain a public definition for 'GetEnumerator'
0
Fernando SotoRetiredCommented:
Can you post the class definition for InvoiceItem please.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

bmanmike39Author Commented:
Sorry I Don't know where to fined that, would that be in siteModel.context.tt?
0
Fernando SotoRetiredCommented:
Under the other tt file extension.
0
bmanmike39Author Commented:
public partial class InvoiceItem
{
    public int InvoiceItemID { get; set; }
    public Nullable<int> InvoiceNumber { get; set; }
    public Nullable<int> InvoiceID { get; set; }
    public Nullable<int> ItemNumberID { get; set; }
    public string ItemName { get; set; }
    public Nullable<decimal> ItemPrice { get; set; }
    public Nullable<int> Pk_Qt { get; set; }
    public string Type { get; set; }
    public string Size { get; set; }
    public string Color { get; set; }
    public string Descritption { get; set; }
    public Nullable<bool> PublicNLA { get; set; }
    public Nullable<int> Quantity { get; set; }
    public string image { get; set; }
    public Nullable<decimal> TotalCost { get; set; }
    public Nullable<int> TotalPkQt { get; set; }
}
0
Fernando SotoRetiredCommented:
OK, none of them is a collection of some object. So which item do you want to insert into the body of the email. If that is a string does that string have multiple value  that may be separated by some character.
0
bmanmike39Author Commented:
I want to show a list of ItemName, ItemPrice, and Pk_Qt, of each item.
0
bmanmike39Author Commented:
And no each item is just singular.
0
Fernando SotoRetiredCommented:
You state that you want to show a List of the following fields from the class InvoiceItem. But according to your original post you are returning one instance of the class InvoiceItem from the query. So you only have one itemName and zero or one of the other two fields, Is that correct?

public string ItemName { get; set; }
public Nullable<decimal> ItemPrice { get; set; }
public Nullable<int> Pk_Qt { get; set; }
0
bmanmike39Author Commented:
I want to return all of the invoice item.  I noticed that it says .Last I think it was suppose to be List.  But yes, I'm trying to returne the 3 field from each  recorde of invoiceItems
0
Fernando SotoRetiredCommented:
OK, you are wishing to return all invoiceItems that have a InvoiceNumber == v, v is being supplied by the user. Is that correct?
0
bmanmike39Author Commented:
Yes
0
Fernando SotoRetiredCommented:
Try this code it should give you what you need.

MyEntities2 m = new MyEntities2();
List<InvoiceItem> mi = m.InvoiceItems.Where(x => x.InvoiceNumber == v).ToList();

string body = "";
foreach(invice item in mi)
{
    // You will need to format the string maybe using String.Format()
    body += item.ItemName.ToString() + "   " + 
                   item.ItemPrice.ToString() + "   " + 
                   item.Pk_Qt.ToString() + "   " + "<br/>";
}

msg.From = address;
msg.Subject = "My Products";
msg.Body = body;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bmanmike39Author Commented:
Thank you very much!!! That worked.
0
Fernando SotoRetiredCommented:
Not a problem bmanmike39, glad I was able to help.
0
bmanmike39Author Commented:
Thanks again for the help.  I have a better understanding of how this work now.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.