Link to home
Start Free TrialLog in
Avatar of bmanmike39
bmanmike39

asked on

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

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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

Avatar of bmanmike39
bmanmike39

ASKER

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'
Can you post the class definition for InvoiceItem please.
Sorry I Don't know where to fined that, would that be in siteModel.context.tt?
Under the other tt file extension.
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; }
}
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.
I want to show a list of ItemName, ItemPrice, and Pk_Qt, of each item.
And no each item is just singular.
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; }
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
OK, you are wishing to return all invoiceItems that have a InvoiceNumber == v, v is being supplied by the user. Is that correct?
Yes
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much!!! That worked.
Not a problem bmanmike39, glad I was able to help.
Thanks again for the help.  I have a better understanding of how this work now.