开发者

Update one object with another in LINQ

开发者 https://www.devze.com 2023-01-29 18:53 出处:网络
I use LINQ to expose database objects to my appl开发者_开发知识库ication.Also in this application I get an object from my database called \'foo\' like so.

I use LINQ to expose database objects to my appl开发者_开发知识库ication. Also in this application I get an object from my database called 'foo' like so.

public static Foo Get(int id)
{
    MyDataContext db = new MyDataContext ();
    Foo foo1 = (from f in db.Foos
        where f.foo_id = id
        select f).SingleOrDefault();
    return foo1;
}

Now, if I change foo and want to update it without going through each field explicitly - I want to avoid this.

public static Foo Update(Foo foo)
{
    MyDataContext db = new MyDataContext ();
    Foo foo1 = (from f in db.Foos
        where f.foo_id = foo.foo_id
        select f).SingleOrDefault();

    if(foo1 != null)
    {
        foo1.foo_nm = foo.foo_nm;
        ... 

        db.SubmitChanges();
    }
}

Is there a way I can do something like in the entity framework:

foo1.UpdateVales(foo);


Create your own cloning method that copies all the properties you're interested in.

Alternatively you can use reflection to find all public properties and loop through them.

LINQ is no help here.


Here's a very simple (and also inefficient) extension function that can get you started (Make sure you import System.Reflection and System.Linq):

    public static void GetDataFrom<T>(this T ObjToCopyTo, T ObjToCopyFrom) where T : class {
        foreach (var property in ObjToCopyTo.GetType().GetProperties().Where(x => x.CanWrite))
            property.SetValue(ObjToCopyTo, property.GetValue(ObjToCopyFrom, null), null);
    }

You will probably find that you will have to put some special cases in for things like the primary keys, and may decide you do not want to copy every property exactly, but hopefully this shows the idea behind VVS's answer.

If you have linqpad installed you can play around with this idea with the following script:

void Main()
{
    var foo = new Helper.Foo() {
        field1 = "hi",
        field2 = 5,
        field3 = 3.14,
        field4 = 'm'
    };
    var foo2 = new Helper.Foo();
    foo.Dump("Foo Before");
    foo2.Dump("Foo2 Before");
    foo2.GetDataFrom(foo);
    foo.Dump("Foo After");
    foo2.Dump("Foo2 After");
}

public static class Helper {

    public class Foo {
        public string field1 { get; set; }
        public int field2 { get; set; }
        public double field3 { get; set; }
        public char field4 { get; set; }
    }

    public static void GetDataFrom<T>(this T ObjToCopyTo, T ObjToCopyFrom) where T : class {
        foreach (var property in ObjToCopyTo.GetType().GetProperties().Where(x => x.CanWrite))
            property.SetValue(ObjToCopyTo, property.GetValue(ObjToCopyFrom, null), null);
    }

}

I hope this helps!


I use automapper to avoid the heavy lifting to update the DB with MVC models, should work for mapping to the same object type and comes in handy for these sorts of coding styles.

After installing automapper ...

public ActionResult Example(TableModel model)
{
    ..
    Mapper.CreateMap<TableModel, Table>();
    Table myTable = new Table();
    Mapper.Map(model, myTable);
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消