Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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#Linq滤波器IEnumerable动态特性和值 公共类示例 { 公共int Id{get;set;} 公共字符串说明{get;set;} public DateTime EffectiveDate{get;set;} } 可编号样本列表; //填充列表_C#_Linq_Filter - Fatal编程技术网

C#Linq滤波器IEnumerable动态特性和值 公共类示例 { 公共int Id{get;set;} 公共字符串说明{get;set;} public DateTime EffectiveDate{get;set;} } 可编号样本列表; //填充列表

C#Linq滤波器IEnumerable动态特性和值 公共类示例 { 公共int Id{get;set;} 公共字符串说明{get;set;} public DateTime EffectiveDate{get;set;} } 可编号样本列表; //填充列表,c#,linq,filter,C#,Linq,Filter,现在我想通过“Id”属性,有时是“Description”属性来过滤列表。只需要将属性名(filterColumn)和属性值(filterValue)都作为字符串传递 我尝试了以下方法: public class Sample { public int Id { get; set; } public string Description { get; set; } public DateTime EffectiveDate { get; set; } } IEnumberable<Sam

现在我想通过“Id”属性,有时是“Description”属性来过滤列表。只需要将属性名(filterColumn)和属性值(filterValue)都作为字符串传递

我尝试了以下方法:

public class Sample
{
public int Id { get; set; }
public string Description { get; set; }
public DateTime EffectiveDate { get; set; }
}

IEnumberable<Sample> sampleList;
//Populate the list
IEnumerable result=sampleList.Where(x=>x.GetType().GetProperty(filterColumn.Name==filterValue);

string,其中query=string.Format(“{0}=\”{1}\”,filterColumn,filterValue);
IEnumerable result=sampleList.AsQueryable().Where(whereQuery);
第二个选项是有效的,如果我将filterColumn作为“Description”传递,但当“Id”作为filterColumn传递时,在string和int error之间抛出不可压缩的“=”运算符,以及一些filterValue,如“1”


谢谢你的帮助。谢谢

您的第一种方法可以奏效。根据Jon Skeet的评论,这里是调整后的声明

string whereQuery = string.Format(" {0} = \"{1}\"", filterColumn, filterValue);
IEnumerable<Sample> result = sampleList.AsQueryable().Where(whereQuery);
另外,请注意字符串在使用“==”运算符时有一些特殊行为。需要记住的重要一点是,引用(容器)和内容之间存在差异。你想要比较内容,这意味着相等。(我注意到Visual Studio中的即时窗口在使用“==”时,其字符串结果不一致。我怀疑这是因为字符串引用可以在该窗口中进行优化,但并不总是如此。)


您声明第二种方法有效。我在标准IEnumerable.Where方法中没有见过这种类型的筛选器字符串。所以我猜您正在使用一些扩展。您的示例没有如图所示工作。DataTable类使用与您的用法匹配的筛选器字符串。通常,必须根据数据类型以不同的方式构造筛选器字符串。例如,字符串需要引号(您有引号),但整数值不使用引号。

另一个选项是设置包含所需操作的字典

Console.WriteLine(12 == 12); 
// True

object a = 12;
object b = 12;

Console.WriteLine(a == b); 
// False - because, due to boxing, a and b are separate objects
// that happen to contain the same value. (Check out "boxing" 
// if this doesn't make sense.)

Console.WriteLine(a.Equals(b)); 
// True - because the Equals method compares content (value)
public IEnumerable GetFiltered(
IEnumerable示例、字符串属性、字符串值)
{
var map=newdictionary()
{
{“Description”,v=>s=>s.Description==v},
{“Id”,v=>s=>s.Id==int.Parse(v)},
};
返回样本。其中(映射[属性](值));
}

这里的优点是,您可以执行更复杂的比较,例如按值范围添加自定义筛选器,或添加包含多个属性的筛选器。

第一个选项是检查属性名称是否与指定的属性值相同。您需要在PropertyInfo上调用
.GetValue(x,null)
。。。然后你想使用
Equals
而不是
=
。你为什么要做
.AsQueryable()
IEnumerable<Sample> result = sampleList.Where(
  x => x.GetType().GetProperty(filterColumn).GetValue(x, null).Equals(filterValue)
);
public IEnumerable<Sample> GetFiltered(
  IEnumerable<Sample> samples, string filtercolumn, object filtervalue
{ 
   return samples.Where(
      x => x.GetType().GetProperty(filtercolumn).GetValue(x, null).Equals(filtervalue)
   );
}

IEnumberable<Sample> sampleList;

var byId = GetFiltered(sampleList, "Id", 100);
var byDescription = GetFiltered(sampleList, "Description", "Some Value");
Console.WriteLine(12 == 12); 
// True

object a = 12;
object b = 12;

Console.WriteLine(a == b); 
// False - because, due to boxing, a and b are separate objects
// that happen to contain the same value. (Check out "boxing" 
// if this doesn't make sense.)

Console.WriteLine(a.Equals(b)); 
// True - because the Equals method compares content (value)
public IEnumerable<Sample> GetFiltered(
    IEnumerable<Sample> samples, string property, string value)
{
    var map = new Dictionary<string, Func<string, Func<Sample, bool>>>()
    {
        { "Description", v => s => s.Description == v },
        { "Id", v => s => s.Id == int.Parse(v) },
    };
    return samples.Where(map[property](value));
}