检查不同类型的两个列表是否相等

本文关键字:列表 两个 是否 同类型 检查 | 更新日期: 2023-09-27 17:59:45

我在检查列表相等性时遇到问题。我有以下清单:

List<RequestDetailViewModel> requestDetail

RequestDetailViewModel是:

public int PropertiesValueID { get; set; }
public int UnitID { get; set; }
public string Value { get; set; }
public int PropertyID { get; set; }

我有另一个列表"reqList":

var reqList = (from p in db.RequestDetail
                        group new 
                        { p.PropertyID, p.UnitID , p.Value , p.PropertiesValueID }
                         by p.RequestID into reqG
                        select reqG
                       );

我想检查列表的相等性像这样:

foreach (var item in reqList)
{
        if (requestDetail equals item)
        {
            return true;
        }
}

我该如何解决这个问题?

检查不同类型的两个列表是否相等

修改reqList选择:

var reqList = (from p in db.RequestDetail
                      group new 
             { p.PropertyID, p.UnitID , p.Value , p.PropertiesValueID }
               by p.RequestID into reqG
                select new RequestDetailViewModel{
                  PropertyID = reqG.PropertyID, UnitID = reqG.UnitID ,
                  Value = reqG.Value , 
                  PropertiesValueID = reqG.PropertiesValueID 
                });

将返回List<RequestDetailViewModel>

RequestDetailViewModel类实现IComparable,然后使用SequenceEqual比较两个列表

为什么不循环查看模型列表,linq查询每个项目的域列表。类似于:

foreach (var detail in requestDetail)
{
    var reqListEqualItems = (from p in db.RequestDetail
                             where p.PropertyID == details.PropertyID &&
                                   p.UnitID == details.UnitID &&
                                   p.Value == details.Value &&
                                   p.PropertyID == details.PropertyID);
}

类似的东西应该为每个ViewModel项提供域对象项。

我将更新RequestDetailViewModel,如下所示:

public class RequestDetailViewModel : INotifyPropertyChanged
{
  private ResultDetail resultDetail;
  public RequestDetailViewModel(ResultDetail resultDetail)
  {
    this.resultDetail = resultDetail;
  }
  public ResultDetail
  {
     get
     {
       return this.resultDetail;
     }
  }
  public int PropertiesValueID 
  {
    get
    {
     return this.resultDetail.PropertiesValueID;
    }
    set
    {
       this.resultDetail.PropertiesValueID = value;
       this.RaisePropertyChanged("PropertiesValueID");
    }
  }
  public int UnitID 
  {
    get
    {
       return this.resultDetail.UnitID ;
    }
    set
    {
       this.resultDetail.UnitID = value;
       this.RaisePropertyChanged("UnitID");
    }
  }
  public string Value
  {
    get
    {
       return this.resultDetail.Value;
    }
    set
    {
       this.resultDetail.Value= value;
       this.RaisePropertyChanged("Value");
    }
  }
  public int PropertyID 
  {
    get
    {
       return this.resultDetail.PropertyID ;
    }
    set
    {
       this.resultDetail.PropertyID = value;
       this.RaisePropertyChanged("PropertyID");
    }
  }    
}

我也会在你的数据库模型中实现IEquatable。我会这样检查:

foreach (var item in reqList)
{
    if (requestDetail.ResultDetail.Equals(item))
    {
       return true;
    }
}

或者更好的

return reqList.Any(item=> item.Equals(requestDetail.ResultDetail));