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

时间:2013-01-08 18:26:19

标签: c# generic-list

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

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

第二个列表(masterList)是一个匿名IList,我知道它有2个属性{ID,DESC} - ID(可以是int或string)和description(字符串)。我可以在此列表中获取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 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

将此作为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]);
}

如果您不喜欢看到反射效果,也可以在PerformReflectionEqualityCheck AssetClass中使用以下内容

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);