0

I have a table named Category

CatID | Col1 | Col2 | Col3 | Col4 |
----------------------------------
1     | A    | 4    | C    | 3    |
----------------------------------

I retrieve and update the CatID 1 item using the code below

var context = new DBEntities();
var x = (from a in context.Categories where a.CatID == 1 select a).firsordefault();
x.Col1 = "Z";
x.Col4 = 20;
context.SaveChanges();

How would i do it to retrieve only the columns that i updated? Like:

CatID | Col1 | Col4 |
--------------------
1     | Z    | 20   |
--------------------

I would like to do this on tables with more than 10 columns. I am using C# EF5, and SQL2008r2 for the database

0

1 Answer 1

0

You can select new anonymous object using linq like this:

var x = (from a in context.Categories where a.CatID == 1 
              select new {CatID, Col1, Col4}
         ).firsordefault();

EDIT: If you mean what the question (as identified by @Daniel A. White) this is duplicating then disregard above. This will only work if you always know what columns are that you want to select. Answer to that question will allow to select al modified columns

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.