Linq Include from List<string>
本文关键字:string gt Include lt List Linq from | 更新日期: 2023-09-27 18:02:53
是否可以从列表中指定" include " ?
我有一个可能包含的子表列表:
List<string> includedTables
我当前的数据库调用是这样的:
return db.SA_Items
.Where(x => x.LastUpdated > lastUpdated)
.Where(x => x.Active == true)
.OrderBy(x => x.ItemID)
.Skip(offset)
.Take(limit)
.ToList();
我想以某种方式在列表中添加foreach字符串,并将.Include(....)
添加到数据库调用…
return db.SA_Items
.Include("first string in the list")
.Include("second string in the list")
.Where(x => x.LastUpdated > lastUpdated)
.Where(x => x.Active == true)
.OrderBy(x => x.ItemID)
.Skip(offset)
.Take(limit)
.ToList();
List也有可能为空。
是否有可能以某种方式动态地做到这一点?
有谁能给我点建议吗?当然,您可以通过以下几个步骤构建您的查询:
IQueryable<SA_Item> query=db.SA_Items;
if(includedTables!=null)
query = includedTables.Aggregate(query, (current, include) => current.Include(include));
query=query.Where(x => x.LastUpdated > lastUpdated)
.Where(x => x.Active == true)
.OrderBy(x => x.ItemID)
.Skip(offset)
.Take(limit);
return query.ToList();// Materialize your query
你可以做类似的事情发生在下面的stackoverflow链接,但替换int索引已添加到您的字符串:
LINQ: Add RowNumber Column
您可以使用concat。
return db.SA_Items
.Concat(new string[] {"first string in the list", "second string in the list"})
.Where(x => x.LastUpdated > lastUpdated)
.Where(x => x.Active == true)
.OrderBy(x => x.ItemID)
.Skip(offset)
.Take(limit)
.ToList();