使用实体框架的最佳比较算法

本文关键字:最佳 比较 算法 框架 实体 | 更新日期: 2023-09-27 18:17:51

我想知道在创建历史和依赖关系的同时,比较创建的多个对象并将对象的状态更改为Inactive (Deleted)的最佳方法是什么。

这也意味着在一个关系表(MarketCookies)中比较过去和现在的对象。

Id     |    CookieID   |  MarketID  

我发现的丑陋的解决方案是计算我改变了多少对象。
为此,让我们调用过去的项目:ListP
新条目:ListF

我把这个方法分为三个步骤:

1 -计数两个列表;
2 -找到列表F中不存在的列表p中的对象,将其状态更改为Inactive并更新它们;
3 -创建新对象并保存

但是这段代码很难维护…我怎样才能使一个简单的代码维护和保持功能?

市场模式:

 public class Market()
 {
    public ICollection<Cookie> Cookies {get; set;}
 }

饼干模态:

 public class Cookie()
 {
   public int Id {get;set;}
   //Foreign Key
   public int CookieID {get;set}
   //Foreign Key
   public int MarketID {get;set;}
 }
代码:

  public void UpdateMarket (Market Market, int Id)
  {
     var ListP = MarketCookiesRepository.GetAll()
                 .Where(x => x.MarketID == Id && Market.State != "Inactive").ToList();
     var ListF = Market.Cookies.ToList();
     int ListPCount = ListP.Count();
     int ListFCount = ListF.Count();
     if(ListPCount > ListFCount)
     {
         ListP.Foreach(x => 
         {
            var ItemExists = ListF.Where(y => y.Id == x.Id).FirstOrDefault();
            if(ItemExists == null)
            {
               //Delete the Object
            }
         });
         ListF.Foreach(x => 
         {
            var ItemExists = ListP.Where(y => y.Id == x.Id).FirstOrDefault();
            if(ItemExists == null)
            {
               //Create Object
            }
         });
     }
     else if(ListPCount < ListFCount)
          {
              ListF.Foreach(x => 
              {
                 var ItemExists = ListP.Where(y => y.Id == x.Id).FirstOrDefault();
                 if(ItemExists == null)
                 {
                   //Create Objects
                 }
              });
              ListP.Foreach(x => 
              {
                 var ItemExists = ListF.Where(y => y.Id == x.Id).FirstOrDefault();
                 if(ItemExists == null)
                 {
                   //Delete Objects
                 }
              });
          }
          else if(ListPCount == ListFCount)
               {
                   ListP.Foreach(x => 
                   {
                        var ItemExists = ListF.Where(y => y.Id == x.Id).FirstOrDefault();
                        if(ItemExists == null)
                        {
                           //Delete Objects
                        }
                   });
                   ListF.Foreach(x => 
                   {
                       var ItemExists = ListP.Where(y => y.Id == x.Id).FirstOrDefault();
                        if(ItemExists == null)
                        {
                           //Create Objects
                        }
                   });
               }
  }

使用实体框架的最佳比较算法

如果没有一个好的,最小的完整的代码示例来清楚地说明问题,就很难确定一个好的实现是什么样子的,更不用说"最好的"了。但是,根据您的描述,看起来LINQ Except()方法实际上可以很好地满足您的需求。例如:

public void UpdateMarket (Market Market, int Id)
{
    var ListP = MarketCookiesRepository.GetAll()
                .Where(x => x.MarketID == Id && Market.State != "Inactive").ToList();
    var ListF = Market.Cookies.ToList();
    foreach (var item in ListP.Except(ListF))
    {
        // set to inactive
    }
    foreach (var item in ListF.Except(ListP))
    {
        // create new object
    }
}

这当然是假设你的对象已经覆盖了Equals()GetHashCode()。如果没有,您可以为上面的任务提供自己的IEqualityComparer<T>实现。例如:

// General-purpose equality comparer implementation for convenience.
// Rather than declaring a new class for each time you want an
// IEqualityComparer<T>, just pass this class appropriate delegates
// to define the actual implementation desired.
class GeneralEqualityComparer<T> : IEqualityComparer<T>
{
    private readonly Func<T, T, bool> _equals;
    private readonly Func<T, int> _getHashCode;
    public GeneralEqualityComparer(Func<T, T, bool> equals, Func<T, int> getHashCode)
    {
        _equals = equals;
        _getHashCode = getHashCode;
    }
    public bool Equals(T t1, T t2)
    {
        return _equals(t1, t2);
    }
    public int GetHashCode(T t)
    {
        return _getHashCode(t);
    }
}

像这样使用:

public void UpdateMarket (Market Market, int Id)
{
    var ListP = MarketCookiesRepository.GetAll()
                .Where(x => x.MarketID == Id && Market.State != "Inactive").ToList();
    var ListF = Market.Cookies.ToList();
    IEqualityComparer<Cookie> comparer = new GeneralEqualityComparer<Cookie>(
        (t1, t2) => t1.Id == t2.Id, t => t.Id.GetHashCode());
    foreach (var item in ListP.Except(ListF, comparer))
    {
        // set to inactive
    }
    foreach (var item in ListF.Except(ListP, comparer))
    {
        // create new object
    }
}