How do you select specific field from an entity like fNam lName and so on using entity?

I would like to select just specific fields in the db and create a list. I want the select statement to return all the records that have the InvoiceNumber of 1020.

and the only records in the field i want are fName, lName, and Email. This bind it to a dategrid.

How would I write this with entity.
MyEntities1 m = new MyEMEntities1();

int v = 1020;
GridView1.DataSource =  m.Invoice.fName,lName,Email(x => x.InvoiceNumber == v).ToList();
GridView1.DataBind();

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;

Try this, it should give you what you are looking for.

MyEntities1 m = new MyEMEntities1();

int v = 1020;

var results = m.Invoice
               .Where( i => i.InvoiceNumber == v)
               .Select( rec => new
               {
                   rec.fName,
                   rec.lName,
                   rec.Email
               }).ToList();

GridView1.DataSource = results;
GridView1.DataBind();

Open in new window

0
bmanmike39Author Commented:
I get a error == can't be applied to type operand type 'sting' and 'int'
0
Fernando SotoRetiredCommented:
It might be that the invoice number is not and int but a string, they it like this.

MyEntities1 m = new MyEMEntities1();

// This seems to be defined as string in the database and not int
string v = "1020";

var results = m.Invoice
               .Where( i => i.InvoiceNumber == v)
               .Select( rec => new
               {
                   rec.fName,
                   rec.lName,
                   rec.Email
               }).ToList();

GridView1.DataSource = results;
GridView1.DataBind();

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:
Thanks!!
0
Fernando SotoRetiredCommented:
Not a problem bmanmike39, glad I was able to help.
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.