1

我有 2 个列表需要检查传递给通用包装器的常见对象。

第一个列表 ( 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

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

4

2 回答 2

2
var results = LookupList.Select(l => EligibleList.Any(e => e.FEE_ID==l.ID))
                       .ToList();
于 2013-01-08T18:33:16.110 回答
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);
于 2013-01-08T18:48:35.783 回答