简体   繁体   English

从列表中获取请求的列不同值

[英]Get requested column distinct values from list

I have a list with multiple columns. 我有一个包含多列的列表。 I want to filter the list based on the requested column name (column name will come as a parameter) with distinct values. 我想根据具有不同值的请求的列名(列名将作为参数)来过滤列表。

IList<obj1> objTemp= new List<obj1>();
for (int i = 0; i < 15; i++)
{
   obj1 temp= new obj1();
   temp.Name = "Name" + i;
   temp.Age= "Age" + i;
   temp.Company= "Company" + i;
   objTemp.Add(temp);
 }

var distinctTypeIDs = objTemp.Select(x => x.**{my requested column}**).Distinct();

You can use reflection for getting desired property by it's name: 您可以使用反射名称来获得所需的属性:

var distinctTypeIDs = objTemp.Select(x => x.GetType().GetProperty("requested_column").GetValue(x))
                             .Distinct();

One way is to use a method like this. 一种方法是使用这样的方法。

private IList<obj1> SortListAccordingToParameter(string filter)
{
if(filter == "Name")
    return  objTemp.Select(x => x.Name).Distinct();
else if(filter == "Age")
    return objTemp.Select(x => x.Age).Distinct();
else if(filter == "Company")
    return objTemp.Select(x => x.Company).Distinct();
}

If you know the type of the property you will be searching for, you could use expressions. 如果您知道要搜索的属性的类型,则可以使用表达式。

string propName = "Age";
var paramExpression = Expression.Parameter(typeof(Obj1));
// o =>

var memberExpression = Expression.Property(paramExpression, propName);
// o => o.Age

var lambdaExpression = Expression.Lambda<Func<Obj1, string>>(memberExpression, paramExpression);
// (o => o.Age)

var compiled = lambdaExpression.Compile();

IList<Obj1> objTemp = new List<Obj1>();
for (var i = 0; i < 15; i++) {
    Obj1 temp = new Obj1();
    temp.Name = "Name" + i;
    temp.Age = "Age" + i;
    temp.Company = "Company" + i;
    objTemp.Add(temp);
}

var results = objTemp.Select(compiled);
// equivalent to objTemp.Select(o => o.Age), plus a delegate call and the time to 
// compile the lambda. 

I would probably wrap this up in a static class, like this: 我可能会将其包装在一个静态类中,如下所示:

static class Gen<TModel, TProp> {
    public static Func<TModel, TProp> SelectorExpr(string propertyName) {
        var pExpr = Expression.Parameter(typeof (TModel));
        var mExpr = Expression.Property(pExpr, propertyName);
        var lExpr = Expression.Lambda<Func<TModel, TProp>>(mExpr, pExpr);
        return lExpr.Compile();
    }
}

that way you can write your selector like: 这样,您可以像下面这样编写选择器:

var results = objTemp.Select(Gen<Obj1, string>.SelectorExpr(propName));

That seems a bit more clear to me what it is I'm doing, especially if I'm reading expression DOM code I wrote 6 months after. 对我来说,这似乎更清楚我在做什么,尤其是当我正在阅读六个月后编写的表达式DOM代码时。

I've always been a fan of "mapping" a column to an anonymous method responsible for retrieving the contents of that column: 我一直喜欢将列“映射”到负责检索该列内容的匿名方法:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        var items = new List<SomeObject> {new SomeObject { Age = 10, Name = "Daniel", Company = "InCycle" },
                                         {new SomeObject { Age = 20, Name = "Not Daniel", Company = "Not InCycle" }
                                         }};

        var result = Filter<int>(items, "Age");
        Console.WriteLine(result.Last());

    }

    public static IEnumerable<T> Filter<T>(IEnumerable<SomeObject> items, string filterCriteria)
    {
        var mappings = new Dictionary<string, Func<IEnumerable<SomeObject>, IEnumerable<T>>>
        {
            { "Age", filterItems => filterItems.Select(item => item.Age).Distinct().Cast<T>() }, 
            { "Name", filterItems => filterItems.Select(item => item.Name).Distinct().Cast<T>() }, 
            { "Company", filterItems => filterItems.Select(item => item.Company).Distinct().Cast<T>() }
        };

        return mappings[filterCriteria](items);

    }
}

public class SomeObject
{
    public int Age {get;set;}
    public string Name {get;set;}
    public string Company {get; set;}
}

The downside to this approach is that if you add additional properties, you could forget to add them to the filtering. 这种方法的缺点是,如果添加其他属性,则可能会忘记将其添加到过滤中。 Expressions are a solid approach as well. 表达式也是一种可靠的方法。

 public class Test
 {
    public string name { get; set; }
    public string age { get; set; }
    public string contact { get; set; }

    public Test getName(string name)
    {
        List<Test> testList = new List<Test>();

        testList.Add(new Test { name = "Developer", age = "24", contact = "99009900990" });
        testList.Add(new Test { name = "Tester", age = "30", contact = "009900990099" });

        return testList.Where(c => c.name == name).FirstOrDefault();
    }
  }
  static void Main(string[] args)
  {
        Test testObj = new Test();
        Test selectedObj = testObj.getName("Developer");
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM