无法在LINQ查询的where子句中传递多个值

时间:2014-01-30 13:24:58

标签: c# linq list

我有一个LINQ查询,如下所示:

var results=from p in inputTable.AsEnumerable()
          where inputParameter.Contains(p.Field<String>(inputField))&&
                p.Field<string>(CurrencyCode)==currencyCode
          group p by new
          {
           p[CurrencyCode],
          } into groupedTable
          select new
          {
           Amount = groupedTable.Sum(r => r.Field<System.Decimal>(amountField))
          };
if (results.Count() > 0)
            {
                retVal = results.ElementAt(0).Amount;
            }

我的inputParameter基本上是List<string>,其值为{October, November, December}

inputFieldNovember

我的想法是,由于where条件有一个Contains方法,它只会按November过滤行,因为inputFieldNovember

我基本上需要传递列表中的所有元素,即OctoberNovemberDecember,然后获取按这几个月过滤的记录。

我尝试使用where-in的{​​{1}}内容但未成功。

专家请帮助解决这个问题。 任何帮助/指针都会非常值得注意。

修改

让我试着让这个问题变得非常简单。

我的LINQ List<string>可以包含变量字符串,例如inputParameter{October, November, December}等等。

我需要我的查询传递所有这些值并相应地过滤记录。

我尝试的简化查询如下:

{January, February, March, April}

在上面的例子中,我基本上硬编码了List var results=from p in inputTable.AsEnumerable() where p.Field<string>(FiscalMonth)==inputParameter[0] || p.Field<string>(FiscalMonth)==inputParameter[1] || p.Field<string>(FiscalMonth)==inputParameter[2] select new { p.Amount }; 的各个元素,但我的列表有时会变量。即它可以容纳3件物品,4件物品,甚至12件物品。

如何塑造上述查询以避免个别硬编码?

此致

阿努拉格

2 个答案:

答案 0 :(得分:2)

回应您的编辑,应该是:

var results = from p in inputTable.AsEnumerable()
              where inputParameter.Contains(p.Field<string>(FiscalMonth))
              select new
                {
                    p.Amount
                };

这是一个工作控制台示例,演示了Contains的查询:

//create datatable with column FiscalMonth
var table = new DataTable();
table.Columns.Add("FiscalMonth");

//add two rows, January and October
var row1 = table.NewRow();
row1["FiscalMonth"] = "January";
var row2 = table.NewRow();
row2["FiscalMonth"] = "October";
table.Rows.Add(row1);
table.Rows.Add(row2);

//query from data table where FiscalMonth in (October, November, December)
var inputParameter = new List<string> {"October", "November", "December"};
var result = from r in table.AsEnumerable()
             where inputParameter.Contains(r.Field<string>("FiscalMonth"))
             select r.Field<string>("FiscalMonth");

//the result will be only one row, which is October. January is successfully filtered out
foreach (var r in result)
{
    Console.WriteLine(r);
}

答案 1 :(得分:1)

不要真正理解您正在寻找什么,因为您声明只想查询月份,而您的代码正在查询月份和货币代码。但看起来你正在返还金额的总和。所以他是第一次尝试你想做的事情。

class MonthCurrency
    {
        public string Month { get; set; }
        public int CurrencyCode { get; set; }
        public decimal Amount { get; set; }
    }
    static List<MonthCurrency> inputTable  = null;

    static void Main(string[] args){
        inputTable = new List<MonthCurrency>()
        {     new MonthCurrency() { Month = "October", CurrencyCode= 1, Amount = 1},
              new MonthCurrency() { Month = "October", CurrencyCode= 1, Amount = 2},
              new MonthCurrency() { Month = "November", CurrencyCode= 2, Amount = 1},
              new MonthCurrency() { Month = "November", CurrencyCode= 2, Amount = 2},
              new MonthCurrency() { Month = "December", CurrencyCode= 3, Amount = 1},
              new MonthCurrency() { Month = "December", CurrencyCode= 3, Amount = 2},
        };   
        var result = GetCurrencyCode("November");
    }

    static public decimal GetCurrencyCode(string inputParameter)
    {
        decimal retVal = 0.0M;
        var results = from p in inputTable.AsEnumerable()
                      where p.Month == inputParameter
                      group p by new
                      {
                          p.CurrencyCode,
                      } into groupedTable
                      select new MonthCurrency
                      {                              
                          Amount = groupedTable.Sum(r => r.Amount)
                      };
        if (results.Count() > 0)
        {
            retVal = results.ElementAt(0).Amount;
        }
        return retVal;
    }

希望这会帮助你