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

  • SEARCH
  • Home
  • 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 7503887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:23:05+00:00 2026-05-29T21:23:05+00:00

LINQ-to-SQL has been a PITA for me. We’re using it to communicate to the

  • 0

LINQ-to-SQL has been a PITA for me. We’re using it to communicate to the database, and then send entities via WCF to a Silverlight app. Everything was working fine, until it came time to start editing (CUD) the entities, and their related data.

I was finally able to devise two for loops that allowed the CUD. I tried to refactor them, and I was so close, until I learned that I can’t always do Lambda with L2S.

public static void CudOperation<T>(this DataContext ctx, IEnumerable<T> oldCollection, IEnumerable<T> newCollection, Func<T, T, bool> predicate)
    where T : class
{
    foreach (var old in oldCollection)
    {
        if (!newCollection.Any(o => predicate(old, o)))
        {
            ctx.GetTable<T>().DeleteAllOnSubmit(ctx.GetTable<T>().Where(o => predicate(old, o)));
        }
    }

    foreach (var newItem in newCollection)
    {
        var existingItem = oldCollection.SingleOrDefault(o => predicate(o, newItem));
        if (existingItem != null)
        {
            ctx.GetTable<T>().Attach(newItem, existingItem);
        }
        else
        {
            ctx.GetTable<T>().InsertOnSubmit(newItem);
        }
    }
}

Called by:

ctx.CudOperation<MyEntity>(myVar.MyEntities, newHeader.MyEntities,
    (x, y) => x.PkID == y.PkID && x.Fk1ID == y.Fk1ID && x.Fk2ID == y.FK2ID);

This almost worked. However, my Func needs to be an Expression>, and that’s where I’m stuck.

Is there anyone who can tell me if this is possible? We have to be in .NET 3.5 due to SharePoint 2010.

  • 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-29T21:23:06+00:00Added an answer on May 29, 2026 at 9:23 pm

    Just change the parameter from:

     Func<T, T, bool> predicate
    

    To:

     Expression<Func<T, T, bool>> predicate
    

    The Expression is generated by the compiler.

    Now, the issue is how to use this.

    In your case, you need both a Func and an Expression, since you’re using it in Enumerable LINQ queries (func based) as well as the SQL LINQ queries (expression based).

    In:

    .Where(o => predicate(old, o))
    

    The old parameter is fixed. So we could change the parameter to:

    Func<T, Expression<Func<T, bool>>> predicate
    

    This means we can supply one argument (the ‘fixed’ one) and get back an expression.

    foreach (var old in oldCollection)
    {
        var condition = predicate(old);
        // ...
        {
            ctx.GetTable<T>().DeleteAllOnSubmit(ctx.GetTable<T>().Where(condition));
        }
    }
    

    We also need to use this in Any. To get a Func from an Expression we can call Compile():

    foreach (var old in oldCollection)
    {
        var condition = predicate(old);
        if (!newCollection.Any(condition.Compile()))
        {
            ctx.GetTable<T>().DeleteAllOnSubmit(ctx.GetTable<T>().Where(condition));
        }
    }
    

    You can do the same thing with the next part.

    There are two issues:

    1. The performance might be impacted by using Compile() lots. I’m not sure how much effect it would actually have, but I’d profile it to check.
    2. The usage is now a little weird, since this is a curried lambda. Instead of passing (x,y) => ... you will be passing x => y => .... I’m not sure if this is a big deal for you.

    There might be a better way to do this 🙂

    Here’s an alternate method, which should be a bit faster, since the Expression only has to be compiled once. Create a rewriter that will ‘apply’ one argument, like this:

    class PartialApplier : ExpressionVisitor
    {
        private readonly ConstantExpression value;
        private readonly ParameterExpression replace;
    
        private PartialApplier(ParameterExpression replace, object value)
        {
            this.replace = replace;
            this.value = Expression.Constant(value, value.GetType());
        }
    
        public override Expression Visit(Expression node)
        {
            var parameter = node as ParameterExpression;
            if (parameter != null && parameter.Equals(replace))
            {
                return value;
            }
            else return base.Visit(node);
        }
    
        public static Expression<Func<T2,TResult>> PartialApply<T,T2,TResult>(Expression<Func<T,T2,TResult>> expression, T value)
        {
            var result = new PartialApplier(expression.Parameters.First(), value).Visit(expression.Body);
    
            return (Expression<Func<T2,TResult>>)Expression.Lambda(result, expression.Parameters.Skip(1));
        }
    }
    

    Then use it like this:

    public static void CudOperation<T>(this DataContext ctx,
        IEnumerable<T> oldCollection,
        IEnumerable<T> newCollection,
        Expression<Func<T, T, bool>> predicate)
        where T : class
    {
    
        var compiled = predicate.Compile();
    
        foreach (var old in oldCollection)
        {
            if (!newCollection.Any(o => compiled(o, old)))
            {
                var applied = PartialApplier.PartialApply(predicate, old);
                ctx.GetTable<T>().DeleteAllOnSubmit(ctx.GetTable<T>().Where(applied));
            }
        }
    
        foreach (var newItem in newCollection)
        {
            var existingItem = oldCollection.SingleOrDefault(o => compiled(o, newItem));
            if (existingItem != null)
            {
                ctx.GetTable<T>().Attach(newItem, existingItem);
            }
            else
            {
                ctx.GetTable<T>().InsertOnSubmit(newItem);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I love LINQ to SQL but it has been bugging me that in using
I've been programming for a while and have used LINQ-To-SQL and LINQ-To-Entities before (although
Our developer has a linq-2-sql project that talks to my database. The database is
I am using linq to SQL as my ORM and then passing linq To
I have been using LINQ to SQL for years now, but this is the
Has anyone compared the speed of performance between LINQ to SQL against the Entity
I use LINQ-SQL as my DAL, I then have a project called DB which
How to select all columns from tables in join using linq Sql: select CTRL_RUN_JOB.*,
I have a particular scenario where I wrote my code using LINQ-SQL but I
using Linq-to-SQL I'd like to prefetch some data. 1) the common solution is to

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.