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

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
onhax
  • 199
  • 12

1 Answers1

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

TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
  • How can i do this on insert? – onhax Oct 11 '18 at 04:40
  • Please look at the question linked in duplicate section [https://stackoverflow.com/questions/3265257/getting-all-changes-made-to-an-object-in-the-entity-framework] It allows you to pick changed entries and changed properties on those entries. – TheVillageIdiot Oct 11 '18 at 11:34