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
C# Linq中的泛型查询_C#_Linq_Generics_Search - Fatal编程技术网

C# Linq中的泛型查询

C# Linq中的泛型查询,c#,linq,generics,search,C#,Linq,Generics,Search,我想在wpf中创建一个通用的搜索用户控件。我想让它得到一个对象的集合和一个属性的名称来进行搜索。 问题是我不能使用泛型,因为调用搜索函数的代码也不知道类型 有没有办法做到这一点?或者以某种方式查询另一类型下的对象 您可以使用反射 namespace ConsoleApplication2 { class Program { static void Main(string[] args) { var Data = new List<object>() {

我想在wpf中创建一个通用的搜索用户控件。我想让它得到一个对象的集合和一个属性的名称来进行搜索。 问题是我不能使用泛型,因为调用搜索函数的代码也不知道类型

有没有办法做到这一点?或者以某种方式查询另一类型下的对象

您可以使用反射

namespace ConsoleApplication2
{
class Program
{
    static void Main(string[] args)
    {
        var Data = new List<object>() { new A() { MyProperty = "abc" }, new B() { MyProperty = "cde"} };

        var Result = Data.Where(d => (d.GetType().GetProperty("MyProperty").GetValue(d) as string).Equals("abc"));

        // Result is IEnumerable<object> wich contains one A class object ;)
    }
}

class A
{
    public string MyProperty { get; set; }
}

class B
{
    public string MyProperty { get; set; }
}
}
命名空间控制台应用程序2
{
班级计划
{
静态void Main(字符串[]参数)
{
var Data=new List(){new A(){MyProperty=“abc”},new B(){MyProperty=“cde”};
var Result=Data.Where(d=>(d.GetType().GetProperty(“MyProperty”).GetValue(d)作为字符串)。等于(“abc”);
//结果是IEnumerable,其中包含一个类对象;)
}
}
甲级
{
公共字符串MyProperty{get;set;}
}
B类
{
公共字符串MyProperty{get;set;}
}
}
考虑一下这个例子

interface IFoo
    {

    }
    class Bar1 : IFoo
    {
        //interface implementations
        public string Property1 { get; set; }

        public string myProperty1 { set; get; }
    }

    class Bar2 : IFoo
    {
        //interface implementations
        public string Property1 { get; set; }

        public string myProperty1 { set; get; }
    }


    //Search the list of objects and access the original values.
    List<IFoo> foos = new List<IFoo>();
        foos.Add(new Bar1
        {
            Property1 = "bar1",
            myProperty1 ="myBar1"
        });
        foos.Add(new Bar1());

        foos.Add(new Bar2());
        foos.Add(new Bar2());

        //Get the objects.
        foreach (var foo in foos)
        {
            //you can access foo directly without knowing the original class.
            var fooProperty = foo.Property1;

            //you have to use reflection to get the original type and its properties and methods
            Type type = foo.GetType();
            foreach (var propertyInfo in type.GetProperties())
            {
                var propName = propertyInfo.Name;
                var propValue = propertyInfo.GetValue(foo);
            }
        }


var result = list.Where(a => a.propertyName);
接口IFoo
{
}
类Bar1:IFoo
{
//接口实现
公共字符串属性1{get;set;}
公共字符串myProperty1{set;get;}
}
第2类:IFoo
{
//接口实现
公共字符串属性1{get;set;}
公共字符串myProperty1{set;get;}
}
//搜索对象列表并访问原始值。
List foos=新列表();
foos.Add(新的Bar1
{
Property1=“bar1”,
myProperty1=“myBar1”
});
添加(新的Bar1());
添加(新的Bar2());
添加(新的Bar2());
//获取对象。
foreach(foos中的var foo)
{
//您可以在不知道原始类的情况下直接访问foo。
var fooProperty=foo.Property1;
//您必须使用反射来获取原始类型及其属性和方法
Type Type=foo.GetType();
foreach(type.GetProperties()中的var propertyInfo)
{
var propName=propertyInfo.Name;
var propValue=propertyInfo.GetValue(foo);
}
}
var result=list.Where(a=>a.propertyName);

通过使用p.Name.Equals,您假定p是一个具有名为Name的属性的类,并且只按它进行搜索。正如我所解释的,对于泛型,p是一个对象,要搜索的属性只有通过string通过它的名称才能为我所知,如果我理解正确:.GetType().GetProperty(“name”).GetValue(foo)可以吗?我要试一试。此外,上面示例中的数据是可查询的吗?如果我有一个列表,我应该做什么:list.asqueryable().GetType…?逻辑很好,但代码不起作用。第一个d。GetType()返回没有我关心的属性的对象。但是,如果我将它更改为所有类都从中派生的接口,它就会工作。即使如此,查询也不会返回任何结果。如果我将lambda表达式中的“d”替换为(数据[0]),它确实有效。。。这意味着您的示例语法中的某些内容是错误的”以及属性名称“What property?要搜索的属性。例如,如果我搜索Person,我会传递属性名“Full name”。我知道,如果所有类都派生自同一个空接口,我可以在不知道类型的情况下访问该对象。但是List.Where()表达式仍然不可用work@TomerAgmon1,我写的那个不包含Where()。你可以使用它,没有任何问题,我测试了它,它是工作的。你可以得到所有类型及其属性和方法,只使用Refection。因此,它没有回答我的问题。你能详细说明一下使用Where方法吗?我不确定你是否理解。我想按作为输入的属性筛选列表。我知道如何通过输入获取属性的名称,但如何使用Where筛选列表?上面给我的例子不起作用