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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:49:00+00:00 2026-05-18T00:49:00+00:00

Using Linq, trying to select all records not in my local set fails: var

  • 0

Using Linq, trying to select all records not in my local set fails:

var localset = new List<Category>();
localset.Add( new Category { pk1 = 1, pk2 = 1, name = "one" } );
localset.Add( new Category { pk1 = 1, pk2 = 2, name = "two" } );

var dbCategories = dc.Categories;

var diff = dbCategories.Except(localset);

I need to do basic CRUD: delete from the db set when not in my local set, update where exists, and add new when not there.

I would normally do:

delete from Category C where not exists
    ( select null from LocalSet L where C.pk1 = L.pk1 and C.pk2 = L.pk2 )

update Category set name = L.name
from LocalSet L
where L.pk1 = Category.pk1 and L.pk2 = Category.pk2

insert into Category (pk1, pk2, name)
select pk1, pk2, name
from LocalSet L
where not exists (
    select null from Category C where L.pk1 = C.pk1 and L.pk2 = C.pk2 )

Easy enough I thought. However, .Contains seems to be the only method that works with local sets, and it only seems to compare a single field. The database table has composite keys.

Without changing the composite keys, is there any way to accomplish these tasks?

  • 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-18T00:49:01+00:00Added an answer on May 18, 2026 at 12:49 am

    You’ll have to implement a CategoryComparer like the following:

    public class CategoryComparer : IEqualityComparer<Category>
    {
        public bool Equals(Category a, Category b)
        {
            bool result = false;
            if( a.pk1 == b.pk1 && a.pk2 == b.pk2 && a.name == b.name)
                result = true;
            return result;    
        }
    
        public int GetHashCode(Category category)
        {        
            if (Object.ReferenceEquals(category, null)) return 0;        
            return category.pk1.GetHashCode() ^ category.pk2.GetHashCode();
        }
    }
    

    Then call Enumerable.Except:

    var localset = new List<Category>();
    localset.Add( new Category { pk1 = 1, pk2 = 1, name = "one" } );
    localset.Add( new Category { pk1 = 1, pk2 = 2, name = "two" } );
    
    var diff = dc.Categories
                 .AsEnumerable()
                 .Except(localset, new CategoryComparer());
    

    Let me know if this gets you what you need.

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

Sidebar

Related Questions

I'm pretty new to using LINQ so I'm not entirely sure of all the
Trying to read an RSS and select information using Linq but can't seem to
I am pretty new to using LINQ and have been trying it out querying
I am using Entity Framework with Linq to Entities, trying to select some data
I am trying to merge data between two identical schema databases using Linq-to-sql: List<Contact>
I am trying to get decimal value using LINQ in a MVC3 project. How
I am trying to insert a record into a table using Linq but get
I'm trying to do a left join in subsonic 3 using linq but it
So I'm trying to do a simple insert using Linq but i'm running into
I'm using an ADO.NET Entity Model which I'm trying to query using LINQ. The

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.