Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5981821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:59:52+00:00 2026-05-22T21:59:52+00:00

Supposed I have my data context MyDC and a list of objects called MyObject

  • 0

Supposed I have my data context MyDC and a list of objects called MyObject defined like this:

public class MyObject
{
  public int ObjectID {get;set;}
  public byte ObjectState {get;set;}
  public string ObjectInJson {get;set;}
}

I’m writing a query for a table called ObjectsInJsonCache; the column names for this table are the same as the properties of the object. When I receive the list of MyObjects into the function, I want to either insert the item if it’s not in the database or update the item if it already is.

The function for the query looks like this:

public static in CreateJsonCache(List<MyObject> TheListOfobjects)
{
    int NumberOfObjectsLoaded = 0;

    using ( MyDCdefinition MyDC = new MyDCdefinition())
    {
        ObjectsInJsonCache TheTable = new ObjectsInJsonCache();

        //do I use a foreach loop?
        //how do I switch insert/update?

        // this?: MyDC.ObjectsInJsonCache.InsertOnSubmit...
    }
    return NumberOfObjectsLoaded;
}

How do I write the query in linq-to-sql? In particular, if the list contains 1,000 items, I don’t won’t to talk to the DB 1,000 times so I’m wondering if using a foreach loop is the way to go. And what kind of query do I need? An InsertOnSubmit?

Thanks for your suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:59:53+00:00Added an answer on May 22, 2026 at 9:59 pm

    Generally, you would need 2 lists. 1 list which is already in the database and one list to update.

    What you do is that you first is that you get all objects that are not in the database. What to do with these is simple, just InsertAllOnSubmit.

    You will need 2 database hits. First you get all the objects and then you submit changes.

    It’s hard to write complete code from your example, so I’m just going to explain.

    To get all the items that are not in the db, you would take your input list and use Except. You might need a custom comparer if you are not using the object created by Linq. Also, if you have not set the id of the items not in the db, you can just check if ObjectID == default(int);, if it is, it’s a new one. But that depends on your ID handling.

    To get the items that are already in the db, you should check Intersect. Then you loop trough them and then just update them.

    No updates will be sent to the database until you use SubmitChanges.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
Suppose I have a class that processes some data: class SomeClass { public: void
How is data binding in C# WinForms supposed to work when you have a
Suppose I have a class named Data . Another class annotates one of its
Suppose I have 500 rows of data, each with a paragraph of text (like
Suppose I have the following types of data: class Customer { String id; //
I have an order entry page and a corresponding ViewModel (simplified example): public class
my (@keys,@values) = ($text =~ /\{IS\:([a-zA-Z0-9_-]+)\}(.*)\{\\IS\:([a-zA-Z0-9_-]+)\}/g); is supposed to match strings like this {IS:cow}moo{\IS:cow}
I tried simply: public class UIDemoComponent extends UIComponentBase { private String someVariable; // this
This is something I am not sure about in core data. Suppose I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.