相关疑难解决方法(0)

如何以类型安全的方式编写具有列名作为参数的LINQ查询

我正在寻求有关如何使用LINQ以类型安全方式实现此目的的帮助.

我需要在包含许多列的"性能"表上执行搜索.根据为搜索指定的条件,我需要选择列并对具有给定值的列执行搜索.

private static IQueryable<Investment> PerformanceSearch(IQueryable<Investment> investments, **??? searchColumn**, double minValue, double maxValue)
{
  var entity = ExtendedEntities.Current;

  investments = from inv in entity.Investments 
                join performance in entity.Performances on inv.InvestmentID equals perfromance.InvestmentID
                where **performance.searchColumn** >= minValue && **performance.searchColumn** = maxValue
  return investments;
}
Run Code Online (Sandbox Code Playgroud)

现在我正在寻求你的帮助:

  1. 如何以类型安全的方式将列"searchColumn"传递给此方法?我正在考虑创建一个字典对象,以适应某种方式来维护实体框架中的列名.但不知道如何实现这一目标.

  2. 如何使用传递的columnName和应用where子句来执行LINQ查询.

我不能使用If Else或Switch案例如下所示是可能的搜索列表...

/*
 * Search Columns can be:
 *      "Return1Month", "Return2Months", "Return3Months", ... almost 10 more and
 *      "Risk1Month", "Risk2Months", "Risk3Months", ... almost 10 more and
 *      "TrackingError1Month", "TrackingError2Months", "TrackingError3Months", ... almost 10 more …
Run Code Online (Sandbox Code Playgroud)

c# linq lambda linq-to-entities expression

5
推荐指数
1
解决办法
4827
查看次数

标签 统计

c# ×1

expression ×1

lambda ×1

linq ×1

linq-to-entities ×1