Sravanti
Sravanti

Reputation: 1407

How to display only specific columns of a table in entity framework?

how to display the some specific columns of table instead of whole table in entity framework.

using (DataEntities cxt = new DataEntities())
{
    notes note = cxt.notes.Where(no => no.id == accID).SingleOrDefault();
    return notes;
}

Upvotes: 1

Views: 291

Answers (2)

Maxim Zabolotskikh
Maxim Zabolotskikh

Reputation: 3347

You can do this with QueryView. This implies editing your model directly in XML as there is no designer support for this, but you will get an independant entity with less fields than the original one.

Advantages:

  • You can then query data base for this truncated entity directly (you will get only fields you need from the data base - no need to get whole entity from DB and trancate it in code)
  • It is good in scenarios where you want to send this truncated entity to the client with WCF to minimize traffic (e.g. when building big lists on client that basically need only name and ID and no other entity specific information).

Disadvantages:

  • This QueryView-based entity is read only. To make it writeable you will have to add r/w functionality yourself

Upvotes: 0

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13574

For this purpose, I would suggest you to make use of ViewModel like following :-

notes note = cxt.notes.SingleOrDefault(no => no.id == accID);
var model = new YourViewModel // Your viewModel class
            {
                   ID = note.ID,
                   PropertyOne = note.PropertyOne, // your ViewModel Property
                   PropertyTwo = note.PropertyTwo
            };

Upvotes: 2

Related Questions