Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用查询字符串查询字符串列表?_C#_String_List_Dictionary - Fatal编程技术网

C# 使用查询字符串查询字符串列表?

C# 使用查询字符串查询字符串列表?,c#,string,list,dictionary,C#,String,List,Dictionary,我有一本字典: <string,List<string>> 然后我“可以”有一个规则/过滤器列表,例如 var tmpFilter = new customfilters(); tmpFilter.Field = "2"; tmpFilter.Expression = ">"; tmpFilter.Filter = "10"; 因此,对于上面的例子,这将通过,因为在索引2(tmpFilter.Field)处,它大于10;然后我有另一个对象,它定义了列表中我要写入

我有一本字典:

<string,List<string>>
然后我“可以”有一个规则/过滤器列表,例如

var tmpFilter = new customfilters();
tmpFilter.Field = "2";
tmpFilter.Expression = ">";
tmpFilter.Filter = "10";
因此,对于上面的例子,这将通过,因为在索引2(tmpFilter.Field)处,它大于10;然后我有另一个对象,它定义了列表中我要写入文件的字段。对于那个字典条目,我只想写下过滤器匹配的产品品牌和价格

在没有过滤器的情况下,我有:

var tmp = new custom();
tmp.Columns = "0,1";
tmp.Delimiter = ",";
tmp.Extention = ".csv";
tmp.CustomFilters = new List<customfilters>() {new customfilters(){ Field = "2", Expression = ">", Filter = "10"} };

public static void Custom(custom custom)
{
  foreach (var x in Settings.Prods)
  {
    //Get Current Product Code
    var curprod = Settings.ProductInformation[x];// the dictionary value

    foreach (var column in custom.Columns)
    {
      var curVal = curprod[Convert.ToInt32(column)];
      tsw.Write(curVal + custom.Delimiter);
    }
    Settings.Lines++;
    tsw.WriteLine();
  }
  tsw.Close();
}
var tmp=new custom();
tmp.Columns=“0,1”;
tmp.Delimiter=“,”;
tmp.extension=“.csv”;
tmp.CustomFilters=new List(){new CustomFilters(){Field=“2”,Expression=“>”,Filter=“10”};
公共静态无效自定义(自定义)
{
foreach(Settings.Prods中的变量x)
{
//获取当前产品代码
var curprod=Settings.ProductInformation[x];//字典值
foreach(custom.Columns中的var列)
{
var curVal=curprod[转换为32(列)];
tsw.Write(曲线+自定义分隔符);
}
设置.Lines++;
tsw.WriteLine();
}
tsw.Close();
}
我只想在所有过滤器都通过该字符串列表时编写curprod


如何做到这一点?

基于Microsoft发布的一个示例,有一个非常好的Nuget软件包,由于某些原因,他们决定让它很难找到,它允许动态linq查询:

资料来源:

使用它,您可以很容易地完成类似的工作(注意:我在这里使用字符串,因为OP声明它们有一个
列表
):


这可能会给你一个起点。您需要解决的一件事是,在尝试应用过滤器之前,确定哪些字段是数字字段,并且应该转换为数字类型。否则,您将作为字符串进行比较。

您是否已经有了
customFilter
逻辑工作?你是如何将它们应用到你的属性列表中的?我现在没有应用过滤逻辑这是我在选择哪些“产品”时如何应用过滤逻辑的问题要写入文件,customfilters对象只定义了过滤器,它是自定义对象中的一个字段。请参阅my updateStill,不完全清楚您卡在哪个部分上。您是否询问如何仅选择通过筛选的项目(在这种情况下,您可以简单地说
if(applyMyFilters(curprod))
其中
applyMyFilters
是一个函数,它将对通过筛选的
列表应用每个筛选并返回true或false)或者您是在问如何实际使用自定义过滤器本身?换句话说,在您的示例中,如何使
tmpFilter
使用条件
>10
实际测试列表中的第三项?如何实际应用字符串中的测试,以便执行类似curprod[0]的操作。SomeMethodToQuery(“>10”)它返回一个boolOk-所以您实际上要寻找的是如何将
>10
解析为实际的逻辑谓词。
var tmp = new custom();
tmp.Columns = "0,1";
tmp.Delimiter = ",";
tmp.Extention = ".csv";
tmp.CustomFilters = new List<customfilters>() {new customfilters(){ Field = "2", Expression = ">", Filter = "10"} };

public static void Custom(custom custom)
{
  foreach (var x in Settings.Prods)
  {
    //Get Current Product Code
    var curprod = Settings.ProductInformation[x];// the dictionary value

    foreach (var column in custom.Columns)
    {
      var curVal = curprod[Convert.ToInt32(column)];
      tsw.Write(curVal + custom.Delimiter);
    }
    Settings.Lines++;
    tsw.WriteLine();
  }
  tsw.Close();
}
List<string> stuff = new List<string> { "10.40", "64", "5", "56", "99", "2" };

var selected = stuff.Select(s => new { d = double.Parse(s) }).Where("d > 10");

Console.WriteLine(string.Join(", ", selected.Select(s => s.d.ToString()).ToArray()));
10.4, 64, 56, 99