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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:52:32+00:00 2026-06-01T01:52:32+00:00

I have a code like this: class PacketDAO{ //… public void UpdatePacketStatus(Guid packetID, Status

  • 0

I have a code like this:

class PacketDAO{
    //...
    public void UpdatePacketStatus(Guid packetID, Status status)
    {
        using (var ctx = new DataContext())
        {
            var packet = ctx.Packet.SingleOrDefault(p => p.PacketID == packetID);
            packet.Status = status;
            ctx.SubmitChanges();
        }
    }

    public void UpdatePacketTime(Guid packetID, DateTime? time)
    {
        using (var ctx = new DataContext())
        {
            var packet = ctx.Packet.SingleOrDefault(p => p.PacketID == packetID);
            packet.Time = time;
            ctx.SubmitChanges();
        }
    }
    //...
}       

We can notice some boring repetition in the code.

So, it would be nice to write a generic method Update in a way we can afford ourselves to write something like this:

packet.Update<Guid, Packet>(guid, p => p.Time = DateTime.Now);
packet.Update<Guid, Packet>(guid, p => p.Status = Status.Ok);

Tell me, please, is it possible to write such a method?

Which book can I learn that from?

(I have found only one close example: http://msdn.microsoft.com/en-us/library/cc981895.aspx, but it is not clear enough how to derive my Update method from that)

Thank you.

UPD.

Ok, Jon Skeet tells there’s something wrong in the question, and I agree, that my calls should look different, I think these calls are possible:

packet.Update<Packet>(p => p.packetID == guid, p => p.Time = DateTime.Now);
packet.Update<Packet>(p => p.packetID == guid, p => p.Status = Status.Ok);
  • 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-06-01T01:52:34+00:00Added an answer on June 1, 2026 at 1:52 am

    Lets start by writing an ordinary helper function. This has nothing to do with LINQ.

    public static void UpdatePacket(Guid packetID, Action<Packet> update)
    {
        using (var ctx = new DataContext())
        {
            var packet = ctx.Packet.SingleOrDefault(p => p.PacketID == packetID);
            update(packet);
            ctx.SubmitChanges();
        }
    }
    

    So you see, you can use the update-delegate to extract the only small piece of code which is different for each call. The rest is the same, and now we have it centralized and reusabe.

    You can also make the method generic:

    public static void UpdatePacket<TEntity>(Expression<Func<TEntity, bool>> filter, Action<TEntity> update)
    {
        using (var ctx = new DataContext())
        {
            var e = ctx.GetTable<TEntity>().Single(filter);
            update(e);
            ctx.SubmitChanges();
        }
    }
    

    If you want to have the filter automated, you need to use the Expression and reflection API to construct the filter expression. The code for that is a little longer.

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

Sidebar

Related Questions

I have code that looks like this: public class A { public void doStuff()
I have code like this: namespace N { class B { public: virtual void
I have code like this: class Base { public: void operator = (const Base&
I have code like this: public class Class1 { public void method1() { ...
Suppose I have some code like this: class Visitor { public: Visitor(callBackFunction) {} void
I have code like this... class Time { public: Time(int, int, int); void set_hours(int);
I have a similar code snippet like this class Search { public function search($for,
I have code that looks like this: template<class T> class list { public: class
my code is like this: i have two classes first class: public class Box<E>
I have code that looks like this: public class Polynomial { List<Term> term =

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.