如何检查2个通用列表之间的公共对象

本文关键字:列表 之间 公共对象 2个 何检查 检查 | 更新日期: 2023-09-27 17:51:05

我有两个列表,我需要检查正在传递给通用包装器的公共对象。

第一个列表(selList)是一个类型化实体列表。此列表中的ID字段是不同的,这取决于正在创建的列表的基本类型。

第二个列表(masterList)是一个匿名的IList,我知道有2个属性{ID, DESC} - ID(可以是int或字符串)和描述(字符串)。我可以在这个列表中获取ID属性的值。

我想返回一个主列表的扩展,该扩展具有一个布尔字段,指示主列表中的项目是否包含在selList中。

我想我是在访问者模式的某个地方。

 public class SelectionCriteria<T> : where T : class
 {
    public IList<T> EligibleList { get; private set; }
    public IList LookupList { get; private set; }
 }
 LookupList = new List<object>
              {
                  new { ID = "fid", DESC = "Record 1"},
                  new { ID = "Record2", DESC = "Record 2"},
                  new { ID = "Record3", DESC = "Record 3"},
                  new { ID = "Record4", DESC = "Record 4"},
              };
  EligibleList = new List<AssetClass>
                 {
                     new AssetClass { FEE_ID = "fid", ASSET_CLASS = "A" },
                 };

我应该得到以下结果:

  LookupList[0] == true
  LookupList[1] == false
  LookupList[2] == false
  LookupList[3] == false

有更好的方法来解决这个问题吗?

如何检查2个通用列表之间的公共对象

var results = LookupList.Select(l => EligibleList.Any(e => e.FEE_ID==l.ID))
                       .ToList();

使用此作为SelectionCriteria<T>的定义

public class SelectionCriteria<T>
    where T : class
{
    public IList<T> EligibleList { get; private set; }
    public IList LookupList { get; private set; }
    public SelectionCriteria(IList lookupList, IList<T> eligibleList)
    {
        LookupList = lookupList;
        EligibleList = eligibleList;
    }
    public bool this[int index]
    {
        get
        {
            var element = LookupList[index];
            foreach (var item in EligibleList)
            {
                if (item.Equals(element))
                {
                    return true;
                }
            }
            return false;
        }
    }
}

这是AssetClass

的定义
public class AssetClass : IEquatable<AssetClass>
{
    public string FEE_ID { get; set; }
    public string ASSET_CLASS { get; set; }
    public bool Equals(AssetClass other)
    {
        return !ReferenceEquals(other, null) && other.FEE_ID == FEE_ID && other.ASSET_CLASS == ASSET_CLASS;
    }
    //Check to see if obj is a value-equal instance of AssetClass, if it's not, proceed
    //  to doing some reflection checks to determine value-equality
    public override bool Equals(object obj)
    {
        return Equals(obj as AssetClass) || PerformReflectionEqualityCheck(obj);
    }
    //Here's where we inspect whatever other thing we're comparing against
    private bool PerformReflectionEqualityCheck(object o)
    {
        //If the other thing is null, there's nothing more to do, it's not equal
        if (ReferenceEquals(o, null))
        {
            return false;
        }
        //Get the type of whatever we got passed
        var oType = o.GetType();
        //Find the ID property on it
        var oID = oType.GetProperty("ID");
        //Get the value of the property
        var oIDValue = oID.GetValue(o, null);
        //If the property type is string (so that it matches the type of FEE_ID on this class
        //   and the value of the strings are equal, then we're value-equal, otherwise, we're not
        return oID.PropertyType == typeof (string) && FEE_ID == (string) oIDValue;
    }
}

您可以获取在查找项列表中存在的符合条件的项列表中找到的元素,如下所示:

for (var i = 0; i < assetClassSelectionCriteria.LookupList.Count; ++i)
{
    Console.WriteLine("LookupList[{0}] == {1}", i, assetClassSelectionCriteria[i]);
}

你也可以使用以下PerformReflectionEqualityCheckAssetClass如果你不喜欢看到反射的好处

private bool PerformReflectionEqualityCheck(object o)
{
    if (ReferenceEquals(o, null))
    {
        return false;
    }
    dynamic d = o;
    try
    {
        return FEE_ID == (string) d.ID;
    }
    catch
    {
        return false;
    }
}

如果"主列表的扩展"指的是扩展方法,那么,您可以这样做,而不是在SelectionCriteria<T>上声明索引器来获得结果:

public static class SelectionCriteriaExtensions
{
    public static bool IsLookupItemEligible<T>(this SelectionCriteria<T> set, int index)
        where T : class
    {
        var element = set.LookupList[index];
        foreach (var item in set.EligibleList)
        {
            if (item.Equals(element))
            {
                return true;
            }
        }
        return false;
    }
}

,并像这样命名:

assetClassSelectionCriteria.IsLookupItemEligible(0);