如何在LINQ表达式中选择多个类属性?
如果我有这样的课如何在LINQ表达式中选择多个类属性?,linq,lambda,Linq,Lambda,如果我有这样的课 ` class Person { public string First; public string Last; public bool IsMarried; public int Age; }` 那么我如何编写一个LINQ表达式来选择一个人的属性呢。我想这样做(用户可以输入1..n个属性) SelectData(x=>x.First,x.Last,x.Age); 我的SelectData函数的输入表达式是什么 SelectDa
`
class Person
{
public string First;
public string Last;
public bool IsMarried;
public int Age;
}`
那么我如何编写一个LINQ表达式来选择一个人的属性呢。我想这样做(用户可以输入1..n个属性)
SelectData(x=>x.First,x.Last,x.Age);
我的SelectData函数的输入表达式是什么
SelectData(Expression<Func<TEntity, List<string>>> selector); ?
SelectData(表达式选择器)?
编辑
在SelectData函数中,我希望提取属性名称,然后动态生成SQL查询的SELECT子句
解决方案
好的,我所做的就是将SelectData设置为
public IEnumerable<TEntity> SelectData(Expression<Func<TEntity, object>> expression)
{
NewExpression body = (NewExpression)expression.Body;
List<string> columns = new List<string>();
foreach(var arg in body.Arguments)
{
var exp = (MemberExpression)arg;
columns.Add(exp.Member.Name);
}
//build query
public IEnumerable SelectData(表达式)
{
NewExpression body=(NewExpression)expression.body;
列表列=新列表();
foreach(body.Arguments中的变量arg)
{
var exp=(MemberExpression)arg;
columns.Add(exp.Member.Name);
}
//生成查询
使用它,我这样称呼它
ccc<Person>().SelectData(x => new { x.First, x.Last, x.Age });
ccc().SelectData(x=>new{x.First,x.Last,x.Age});
希望它能帮助正在寻找的人:)
谢谢,
IY使用和可以获得类似的结果 型号:
namespace ConsoleApplication2
{
class Person
{
public string First { get; set; }
public string Last { get; set; }
public bool IsMarried { get; set; }
public int Age { get; set; }
}
}
服务:
using System.Collections.Generic;
using System.Linq;
namespace Test
{
public static class Service
{
public static IQueryable<IQueryable<KeyValuePair<string, object>>> SelectData<T>(this IQueryable<T> queryable, string[] properties)
{
var queryResult = new List<IQueryable<KeyValuePair<string, object>>>();
foreach (T entity in queryable)
{
var entityProperties = new List<KeyValuePair<string, object>>();
foreach (string property in properties)
{
var value = typeof(T).GetProperty(property).GetValue(entity);
var entityProperty = new KeyValuePair<string, object>(property, value);
entityProperties.Add(entityProperty);
}
queryResult.Add(entityProperties.AsQueryable());
}
return queryResult.AsQueryable();
}
}
}
我认为最好使用委托而不是反射。委托会更快,除此之外,如果您试图获取不存在的属性值,编译器会抱怨。使用反射,您在运行时之前不会发现错误 幸运的是,已经有类似的东西了。它是作为IEnumerable的扩展函数实现的,并且被称为(讽刺的是) 我想你想要这样的东西:
IEnumerable<TResult> SelectData<TSource, TResult>(this IEnumerable<TSource> source,
Func<TSource, TResult> selector)
{
return source.Select(selector);
}
我有一系列的人,我想让你创建一个Linq
语句,该语句每人返回一个包含
首先是属性,最后是属性
var result = Persons.SelectData(person => new
{
First = person.First,
Last = person.Last,
});
或:
我有一系列的人,我想让你创建一个Linq语句
每人返回一个新对象,该对象包含年龄、IsMarried、,
无论是成年人,都很难做到:一个叫做
名称,它是第一个和最后一个的组合
var result = Persons.SelectData(person => new
{
Age = person.Name,
IsMarried = person.IsMarried,
IsAdult = person.Age > 21,
Name = new
{
First = person.First,
Last = person.Last,
},
});
SelectData函数如下所示:
IEnumerable<TResult> SelectData<TSource, TResult>(this IEnumerable<TSource> source,
Func<TSource, TResult> selector)
{
return source.Select(selector);
}
问题2:每人返回一个新对象,该对象包含年龄、是否已结婚、他是否是成年人以及一个名为“姓名”的财产,这是一个组合
首尾
var result = Persons.SelectData(person => new
{
Age = person.Name,
IsMarried = person.IsMarried,
IsAdult = person.Age > 21,
Name = new
{
First = person.First,
Last = person.Last,
},
});
好吧,让我们面对现实,你的SelectData只不过是可枚举的
当然,您可以创建一个函数,让调用者提供他想要的属性列表,但是(1)这会限制他设计最终结果的可能性,(2)他调用该函数需要更多的输入
而不是:
.Select(p => new
{
P1 = p.Property1,
P2 = p.Property2,
}
他必须键入类似的内容
.SelectData(new List<Func<TSource, TResult>()
{
p => p.Property1, // first element of the property list
p -> p.Property2, // second element of the property list
}
你会从中得到什么
非常不鼓励的要求!在询问之前您是否搜索过任何内容?(提示-选择多个)我不想选择数据。我想获取用户在表达式中输入的属性。根据这些属性名称,我想为我的SQL查询生成select子句。我想这不完全是我要寻找的,但它仍然提供了一个解决方案。
.SelectData(new List<Func<TSource, TResult>()
{
p => p.Property1, // first element of the property list
p -> p.Property2, // second element of the property list
}
.Select(p => p.First + p.Last)