作者:Lily賈麗 | 来源:互联网 | 2023-01-05 12:51
我试图在LINQ select语句中使用变量.
这是我现在正在做的一个例子.
using System;
using System.Collections.Generic;
using System.Linq;
using Faker;
namespace ConsoleTesting
{
internal class Program
{
private static void Main(string[] args)
{
List listOfPersOns= new List
{
new Person(),
new Person(),
new Person(),
new Person(),
new Person(),
new Person(),
new Person(),
new Person(),
new Person(),
new Person(),
new Person()
};
var firstNames = Person.GetListOfAFirstNames(listOfPersons);
foreach (var item in listOfPersons)
{
Console.WriteLine(item);
}
Console.WriteLine();
Console.ReadKey();
}
public class Person
{
public string City { get; set; }
public string CountryName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Person()
{
FirstName = NameFaker.Name();
LastName = NameFaker.LastName();
City = LocationFaker.City();
CountryName = LocationFaker.Country();
}
public static List GetListOfAFirstNames(IEnumerable listOfPersons)
{
return listOfPersons.Select(x => x.FirstName).Distinct().OrderBy(x => x).ToList();
}
public static List GetListOfCities(IEnumerable listOfPersons)
{
return listOfPersons.Select(x => x.FirstName).Distinct().OrderBy(x => x).ToList();
}
public static List GetListOfCountries(IEnumerable listOfPersons)
{
return listOfPersons.Select(x => x.FirstName).Distinct().OrderBy(x => x).ToList();
}
public static List GetListOfLastNames(IEnumerable listOfPersons)
{
return listOfPersons.Select(x => x.FirstName).Distinct().OrderBy(x => x).ToList();
}
}
}
}
我有一些非常不干的代码与GetListOf ...方法
我觉得我应该可以做这样的事情
public static List GetListOfProperty(
IEnumerable listOfPersons, string property)
{
return listOfPersons.Select(x =>x.property).Distinct().OrderBy(x=> x).ToList();
}
但这不是虚拟代码.我认为关键在于创造一个功能
如果这就是答案我该怎么做?
这是使用refelection的第二次尝试但是这也是不行的.
public static List GetListOfProperty(IEnumerable
listOfPersons, string property)
{
Person person = new Person();
Type t = person.GetType();
PropertyInfo prop = t.GetProperty(property);
return listOfPersons.Select(prop).Distinct().OrderBy(x =>
x).ToList();
}
我认为反射可能是一个DeadEnd /红鲱鱼,但我想我还是会展示我的作品.
注意实际上简化了示例代码,它用于通过AJAX 填充数据列表以创建自动完成体验.该对象具有20多个属性,我可以通过编写20多种方法来完成,但我觉得应该有一种干燥的方法来完成它.同样制作这个方法也会清理我的控制器动作.
题:
鉴于代码的第一部分有一种方法将这些类似的方法抽象为单个方法,请将一些对象传递给select语句???
感谢您的时间.
1> Marc Gravell..:
您将必须建立选择
.Select(x =>x.property).
用手。幸运的是,这并不是一个棘手的问题,因为您希望它始终是同一类型(string
),因此:
var x = Expression.Parameter(typeof(Person), "x");
var body = Expression.PropertyOrField(x, property);
var lambda = Expression.Lambda>(body, x);
然后Select
上面变成:
.Select(lambda).
(基于的LINQ IQueryable
)或
.Select(lambda.Compile()).
(基于的LINQ IEnumerable
)。
请注意,您可以采取任何措施来缓存最终表单property
。