0

I have an empty column in SQL Table that I want to update.

List<UriDataModel> channelData = new List<UriDataModel>();
for (int i = 0; i < channelNames.Count(); i++)
{
    channelData.Add(new UriDataModel()
    {   SerialNumber = serialNumber[i],                 
        ChannelType = channelUom[i],
        Calculated = calculated[i]
    });
};

I don't want to insert new records, I want to update the column in a row that has other data. There could be around 80,000 records per channel name.

The serial number is unique and will contain one or more channels. Such that serial number 321654, may have six channels, called channel1, channel2 etc. The calculated field is a bool

When I do this:

db.UriData.AddRange(channelData);
db.SaveChanges();

Obviously, this will just add a new record.

I need to find the record from the DB, by saying something like and updating the record.

UPDATE ChannelType, Calculated
WHERE SerialNumber ='987654'
AND ChannelName = 'channel1'

I'm thinking of just firing a SPROC, but I would like to understand how to do it in LINQ.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
The OrangeGoblin
  • 764
  • 2
  • 7
  • 27
  • Possible duplicate of [How to update record using Entity Framework 6?](https://stackoverflow.com/questions/25894587/how-to-update-record-using-entity-framework-6) – mjwills Feb 10 '19 at 20:28
  • I can update a single record, but I have a list of objects, this is what is stumping me. – The OrangeGoblin Feb 10 '19 at 20:38
  • In your `for` loop, don't do `channelData.Add(new UriDataModel()`. Do what is shown in the duplicate. Doing things repeatedly is the same as doing one of them - you just need to put it in a loop. – mjwills Feb 10 '19 at 20:39
  • Is that saying I have to load the entire dataset from SQL so linq can update it? – The OrangeGoblin Feb 10 '19 at 20:43
  • 1
    That is one option yes. You haven't made it clear enough in your question as to some example input data and output data for sure. _If, for example, you are updating most of the rows with the same value you should instead just ran a single `UPDATE TableName SET ColumnName = 'Value'` instead._ – mjwills Feb 10 '19 at 20:44
  • Possible duplicate – Bilaal Rashid Feb 10 '19 at 20:52

0 Answers0