[英]How can I combine two LINQ select expressions?
我試圖有一個 select 表達式,可以根據我從輸入中收到的內容進行增量更新,如下所示:
// Init expression
Expression<Func<Order, object>> selectExpression = x => new
{
x.Id
};
if(selectInput.Title){
// something like this
selectExpression = selectExpression.Add(x => new
{
x.Title
});
}
if(selectInput.ClientFullName){
// something like this
selectExpression = selectExpression.Add(x => new
{
ClientFullname = x.Client.Fullname
});
}
// Use expression in a LINQ query (for an EF Core query)
var result = await queryable.Select(selectExpression).ToListAsync();
然后我希望有這樣的結果
{
"Id": 568,
"Title": "Order 567",
"ClientFullname": "John Smith"
}
這是可能的嗎? 我在網上找到的唯一例子是 about.Where()。 我走錯方向了嗎?
謝謝!
如果您這樣做,那么問題是,您的編譯器將不知道屬性ClientFullName
是否是結果中的屬性,因此您的編譯器無法使用它。
當然,您可以使用屬性的名稱作為標識符,並將所有內容放入字典中。 就像@Canton7 建議的那樣。
如果您不期望 null 值,則可以返回具有所有屬性的 class。 將不會選擇具有值 null 的屬性:
class MyItem
{
public int Id {get; set;} // Id always fetched
public string Title {get; set;}
public DateTime? Date {get; set;}
... // etc: all nullable properties
}
作為擴展方法:
public static IQueryable<MyItem> ToSelectedItems(
this IQueryable<Order> orders,
SelectedInput selectedInput)
{
// TODO: exception if orders or selectedInput null
return orders.Select(order => new MyItem
{
Id = order.Id,
// properties null if not in selectedInput
Title = selectedInput.Title ? order.Title : null,
ClientFullName = selectedInput.ClientFullName ? order.ClientFullName : null,
Date = selectedInput.Date ? order.Date : null,
})
您可能會發現 LinqKit 很有用:( http://www.albahari.com/nutshell/linqkit.aspx )在嘗試針對 IQueryable 數據源編寫變量 where 子句時,我已經使用了很多次。 PredicateBuilder ( http://www.albahari.com/nutshell/predicatebuilder.aspx ) 對於構建動態查詢表達式非常有用。
示例代碼將是這樣的......
public void QueryItems(string id, string name, string fullname)
{
// this would be the collection od data to be filtered
var items = new List<Item>();
// initialise predicate of for querying objects of type Item
var predicate = PredicateBuilder.New<Item>();
// dynamically add clauses dependent on available filter values
if (!string.IsNullOrEmpty(id))
{
predicate = predicate.And(x => x.Id == id);
}
if (!string.IsNullOrEmpty(name))
{
predicate = predicate.And(x => x.Name == name);
}
if(!string.IsNullOrEmpty(fullname))
{
predicate = predicate.And(x => x.FullName == fullname);
}
// evaluate the result of the dynamic query
var result = items.Where(predicate).ToList();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.