c#在类LINQ方法中将类变量作为参数传递给?

时间:2018-02-14 12:26:29

标签: c# .net linq reflection

我有一个包含许多不同字符串值的类

我有一个集合类,包含一些排序函数。目前我有很多重现的代码,用于" GetListOfDifferentProducts" " GetListOfDifferentManufacturers"等

这可能吗?或者我是以错误的方式解决这个问题。

这些都是简单的foreach循环

        public List<string> GetListOfDifferentProducts()
    {
        List<string> listOfResults = new List<string>();
        foreach (Product prod in listOfProducts)
        {
            if (listOfResults.Contains(prod.Name.ToLower()) == false)
                listOfResults.Add(prod.Name.ToLower());
        }
        return listOfResults;
    }

我想传递一个类变量(比如LINQ?)

    public List<string> GetListOfDifferentVariables(variableType)
    {
        List<string> listOfResults = new List<string>();
        foreach (Product prod in listOfProducts)
        {
            if (listOfResults.Contains(prod.variableType.ToLower()) == false)
                listOfResults.Add(prod.variableType.ToLower());
        }
        return listOfResults;
    }

示例用法:

ProductList.GetListOfDifferentVariables(o => o.Name);

示例输入(变量字符串名称)

  • 苹果
  • 苹果
  • 苹果
  • 香蕉
  • 香蕉

输出

  • 苹果
  • 香蕉

MVC

    class Product
{
    public string Name;
    public string Manufacturer;
    public string Description;
    public string Location;
}
class ProductCollection
{
    List<Product> listOfProducts;

    public List<string> GetListOfDifferentProducts()
    {
        List<string> listOfResults = new List<string>();
        foreach (Product prod in listOfProducts)
        {
            if (listOfResults.Contains(prod.Name.ToLower()) == false)
                listOfResults.Add(prod.Name.ToLower());
        }
        return listOfResults;
    }
}

4 个答案:

答案 0 :(得分:1)

如果您正在寻找不同的名称集,请考虑:

listOfProducts.Select(z => z.Name.ToLower()).Distinct();

类似于variableType:

listOfProducts.Select(z => z.variableType.ToLower()).Distinct();

这将避免您需要编写明确的GetListOfDifferentVariables等方法。

如果您真的想要这些方法,请尝试以下方法:

public List<string> GetDistinctNames()
{
    return GetDistinctProperties(product => product.Name.ToLower());
}


public List<string> GetDistinctProperties(Func<Product, string> evaluator)
{
    return listOfProducts.Select(evaluator).Distinct().ToList();
}

GetDistinctNames通过特定字段(即Name)和感兴趣的操作(即ToLower)。

答案 1 :(得分:0)

string[] tmp =
{
    "Apple",
    "Apple",
    "Apple",
    "Pear",
    "Banana",
    "Banana"
};

string[] tmp2 =
{
    "Pear",
    "Banana"
};

IEnumerable<string> uniqueList = tmp.Distinct(); // Removes doubles
IEnumerable<string> notInTmp2 = tmp.Distinct().Except(tmp2); // could be also interesting ;)

Console.WriteLine(string.Join(", ", uniqueList));
Console.WriteLine(string.Join(", ", notInTmp2));

答案 2 :(得分:0)

创建通用方法:

    public IEnumerable<T2> GetListOfDifferent<T1, T2>(Func<T1,T2> selector, IEnumerable<T1> inputs)
    {
        var result = inputs.Select(selector).Distinct();
        return result;
    }

    public IEnumerable<T2> GetListOfDifferent<T1, T2>(string propertyName, IEnumerable<T1> inputs)
    {
        var paramExp = Expression.Parameter(typeof(T1), "p1");
        var member = Expression.PropertyOrField(paramExp, propertyName);
        var lamdaExp = Expression.Lambda<Func<T1, T2>>(member, new[] { paramExp });
        var selector = lamdaExp.Compile();

        var result = GetListOfDifferent(selector, inputs);
        return result;
    }

用法:

var result =GetListOfDifferent<Product,string>(product => product.Name, items)

 var result = GetListOfDifferent<Product,string>(nameof(Product.Name), items);

答案 3 :(得分:0)

如果要将Property属性作为参数传递,则可以使用Reflection。 您需要稍微调整Product类以获得公共属性。

public class Product
{
    private string _name, _manufacturer;


    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }
    public string Manufacturer
    {
        get
        {
            return _manufacturer;
        }
        set
        {
            _manufacturer = value;
        }
    }

}

然后您可以将GetListOfDifferentVariables函数编写为

public List<string> GetListOfDifferentVariables(string propertyName)
    {
        List<string> listOfResults = new List<string>();
        foreach (Product prod in listOfProducts)
        {
            string propertyValue = (string)prod.GetType().GetProperty(propertyName).GetValue(prod);
            if (listOfResults.Contains(propertyValue.ToLower()) == false)
                listOfResults.Add(propertyValue.ToLower());
        }
        return listOfResults.Distinct().ToList();
    }

您可以使用属性名称

调用该函数
var names = GetListOfDifferentVariables("Name");
var manufacturers = GetListOfDifferentVariables("Manufacturer");