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 4030458
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:28:28+00:00 2026-05-20T11:28:28+00:00

I have some C#/Linq code used to merge data from excel file into db,

  • 0

I have some C#/Linq code used to merge data from excel file into db, which needs better performance.

There are
1. A List read from excel file: List<Score> newScoreList
2. A DB table named Scores, primary keys peopleId and testDate

I need to merge data from the list to the table, and if there is any duplicate data, update it.

My current solution is:

1) Find the duplicate data with this LINQ expression:

var dupliData =  
    from newScore in newScoreList  
    from oldScore in db.Scores  
    where newScore.peopleId == oldScore.peopleId && newScore.testDate == oldScore.testDate  
    select oldScore;  

2) Delete the duplicate data.

db.Scores.DeleteAllOnSubmit(dupliData); 

3) Insert the new data from list.

db.Scores.InsertAllOnSubmit(newScoreList);

Could anybody give me a better solution?

  • 1 1 Answer
  • 1 View
  • 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-20T11:28:28+00:00Added an answer on May 20, 2026 at 11:28 am

    I really hate stored procedures in general, but this is probably a perfect case for using one. My TSQL is rusty, but this should give an idea.

    CREATE PROCEDURE dbo.InsertOrUpdateScore
    (
        @id as Int,
        @date as DateTime,
        @result as varchar(20)
    )
    AS
    
    if not exists(SELECT id FROM Scores WHERE id = @id AND date = @date)
    begin
       INSERT INTO Scores (id, date, result) values (@id, @date, @result)
    end
    else
    begin
       UPDATE Scores
       SET result = @result
       WHERE id = @id AND date = @date
    end
    GO 
    

    Now in your LINQ server browser, select the Score entity, and change its INSERT and UPDATE behaviour to use the stored procedure you just created. Make sure the user accessing the database has EXECUTE permission to the SPROC.

    This should perform quite a bit quicker than your version. You’re trading an IN clause for N SELECTs on an index which may be quicker. However, the result set of the IN clause is not transported back to the client over the network, which could save quite a bit of time.

    Profile exactly how long your method is taking before implementing this, so you can gauge if this is truly quicker.

    I’m not sure if this is the only way to create a Score in your application, but you might want to consider the case where you’re INSERTing a record that doesn’t yet have an ID. You’ll need to modify the SPROC to allow @id as null, and handle the INSERT appropriately.

    Then it should just be:

    db.Scores.InsertAllOnSubmit(newScoreList);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my .net application, I have used LINQ-to-SQL to get some data from the
I have a piece of code which combines an in-memory list with some data
I have compared two queries which fetch some fairly large data from a database
I have some LINQ code that generates a list of strings, like this: var
Can I have some help in improving this linq. I'm basically returning speakers from
I I have a linq-to-sql database in which I want to log some of
I'm trying to rewrite some old SQL into LINQ to SQL. I have a
I have some asp.net code that populates the fields in a LINQ to SQL
I have some code which pulls records out of the database and I'm then
Right, bit of a strange question; I have been doing some linq to XML

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.