【问题标题】:Can anyone help me in completing this generic method to update from one list to another list?谁能帮我完成这个从一个列表更新到另一个列表的通用方法?
【发布时间】:2014-09-11 04:49:09
【问题描述】:

我正在尝试将 2 个列表变量作为参数传递给泛型方法。哪个必须遍历第一个列表的所有属性并将值分配给第二个列表?

我以这个例子为例:

假设我有 Customer 作为下面给出的实体类:

class Customer
{
    public int CustId { get; set; }
    public string Name { get; set; }
    public string custPhoneNumber { get; set; }
}

现在我有 2 个客户类型的列表对象。让第一个列表是来自视图的列表,第二个列表是来自数据库的现有列表。

来自视图的 UI 的第一个列表如下所示。

List<Customer> CustomerListToUpdate = new List<Customer>() 
{
    new Customer { CustId = 1,Name = "XYZ" ,custPhoneNumber = "12345"},
    new Customer { CustId = 2,Name = "XYZ" ,custPhoneNumber = "12345"},
    new Customer { CustId = 3,Name = "XYZ" ,custPhoneNumber = "12345"}
};

来自数据库的第二个列表如下所示

List<Customer> extistingCustomerList = new List<Customer>() 
{
    new Customer { CustId = 1,Name = "abc" ,custPhoneNumber = "56789"},
    new Customer { CustId = 2,Name = "abc" ,custPhoneNumber = "56789"},
    new Customer { CustId = 3,Name = "abc" ,custPhoneNumber = "56789"}
};

现在我正在尝试编写一个通用方法,将上述 2 个列表作为参数,然后将新值分配给现有列表并执行我的数据库上下文对象的 .SaveChanges() 方法。

基本上我无法完成这个方法:

class UpdateMethod<T>
{
    public void updatemethod<T>(T UpdatedCustomerList, T extistingCustomerList)
    {
        foreach (var item in UpdatedCustomerList)
        {
            //I need to assign the values of UpdatedCustomerList to extistingCustomerList
        }
    }
}

【问题讨论】:

  • 你想要通用的部分是什么?
  • @AlexeiLevenkov updatemethod 我想做一个通用方法。
  • @AlexeiLevenkov class UpdateMethod { public void updatemethod(T UpdatedCustomerList, T existingCustomerList) { foreach (var item in UpdatedCustomerList) { //我需要将 UpdatedCustomerList 的值赋给 existingCustomerList } } }
  • @Christos 你删除了你的帖子吗?
  • 为什么要修改现有实体?如果UpdatedCustomerList 中的实体包含您需要的所有数据,您只需将它们附加到您现有的DbContext,EF 将自行管理更新。

标签: c# asp.net-mvc linq entity-framework linq-to-entities


【解决方案1】:

然后您可以将方法更改为:

public void updatemethod<T>(List<T> UpdatedCustomerList, List<T> extistingCustomerList) where T : class
    {
        foreach (var item in UpdatedCustomerList)
        {

            var properties = item.GetType().GetProperties();
            var idProp = properties.First(p => p.Name.EndsWith("Id"));
            var toUpdate = extistingCustomerList.First(c => (int)idProp.GetValue(c) == (int)idProp.GetValue(item));
            foreach (var prop in properties)
            {
                prop.SetValue(toUpdate, prop.GetValue(item));
            }
        }
    }

如果您使用实体框架,您可以这样做来更新数据库,如下所示:

class UpdateMethod<T>
{

    updatemethod<T>(List<T> UpdatedCustomerList, List<T> extistingCustomerList)
    {
        var _context = new YourDbContex() // You db context
        foreach (var item in UpdatedCustomerList)
        {
            _context.Entry(item).State = EntityState.Modified;
        }
        _context.SaveChanges();
    }
}

检查此通用存储库以获取 Entity Framework https://github.com/filipmate/user.crud/blob/master/User.CRUD/Repositories/Repository.cs

【讨论】:

  • 基本上我使用的是 .edmx 类。根据您的说法,我可以在哪里创建此 IEntity 接口,以便它适用于所有 .edmx 文件。我很困惑如何处理:(。
  • 我编辑了我的答案,所以现在它不需要界面了。它有效,但它假设键列以 Id 结尾
  • 非常感谢。我将在明天实施并标记您的解决方案。
  • 请注意,将State 设置为Modified 将更新所有属性,如果我理解得很好,这不是您想要的,这就是为什么我建议使用Attach 来代替.. .
猜你喜欢
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多