C# 字典列表的LINQ过滤查询
我有一个通用Linq查询形式的字典列表。每个字典对象都是一个具有属性的类 “姓名”、“年龄”、“地址” 例如:C# 字典列表的LINQ过滤查询,c#,linq,list,dictionary,dynamic,C#,Linq,List,Dictionary,Dynamic,我有一个通用Linq查询形式的字典列表。每个字典对象都是一个具有属性的类 “姓名”、“年龄”、“地址” 例如: public student class{ public string Name {get;set;} public string age {get;set;} public string address {get;set;} public string property {get;set;} } 所以我有一个学生词典列表,它是以泛型查询的形式出现的 List&l
public student class{
public string Name {get;set;}
public string age {get;set;}
public string address {get;set;}
public string property {get;set;}
}
所以我有一个学生词典列表,它是以泛型查询的形式出现的
List<object>listofStudentDictionaries=new list<object>();
listofStudentDictionaries
{
content.....
.............has all dictionaries of student class............... like
all dictionary content goes inside the listofStudentDictionaries.
Dictionary<string,object>dictionary=new Dictionary<string,object>();
dictionary.add(Name,david);
dictionary.add(age,22);
dictionary.add(address,asfdsdfs);
Dictionary<string,object>dictionary=new Dictionary<string,object>();
dictionary.add(Name,rogers);
dictionary.add(age,20);
dictionary.add(address,zczxc);
Dictionary<string,object>dictionary=new Dictionary<string,object>();
dictionary.add(Name,richard);
dictionary.add(age,17);
dictionary.add(address,gfhghfg);
}
IQueryable<T> query=listofStudentDictionaries.AsQueryable();
我得到这个错误
无法将类型数组的索引应用于类型T的表达式
所有这些都不会返回name=david
所在的实体
如何使用Linq实现这一点。请帮忙。字典不是最好的方法。使用下面类似Linq的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
student.students = new List<student>() {
new student() { Name = "david", age = 22, address = "asfdsdfs"},
new student() { Name = "rogers", age = 20, address = "zczxc"},
new student() { Name = "richard", age = 17, address = "gfhghfg"}
};
List<student> query = student.students.Where(x => x.Name == "david").ToList();
}
}
public class student
{
public static List<student> students {get; set;}
public string Name{get;set;}
public int age{get;set;}
public string address{get;set;}
public string property {get;set;}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
student.students=新列表(){
新学生(){Name=“david”,年龄=22,地址=“asfdsdfs”},
新学生(){Name=“rogers”,年龄=20,地址=“zczxc”},
新学生(){Name=“richard”,年龄=17,地址=“gfhghfg”}
};
List query=student.students.Where(x=>x.Name==“david”).ToList();
}
}
公立班学生
{
公共静态列表学生{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
公共字符串地址{get;set;}
公共字符串属性{get;set;}
}
}
是的,我知道。刚刚给了一个样品。只想使用Linq将筛选器应用于泛型查询。如果您正在创建学生词典,为什么不将学生对象添加到词典中?基本上我不创建学生词典,我正在创建一个由学生实体组成的通用字典,并将所有字典添加到对象列表中。因此,我甚至可以在运行时向它添加动态属性。所以最后将过滤器应用于泛型查询的是我的aim.var query=database.students.Queryable();要将列筛选器应用于查询。此外,我在运行时使用了一些动态属性,因此使用dictionary可以将动态属性与静态属性相结合。然后,最后在泛型查询上应用列筛选器。因此,在这种情况下,我可能需要dictionary,因为它具有动态属性。如果您具有动态属性,则该类将是:List properties{get;set}。使用linq或字典的选择取决于1)学生人数。2)获得结果的速度3)内存使用。对于大量学生,我建议使用SQL Express之类的数据库,您可以从MSDN.com免费下载。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
student.students = new List<student>() {
new student() { Name = "david", age = 22, address = "asfdsdfs"},
new student() { Name = "rogers", age = 20, address = "zczxc"},
new student() { Name = "richard", age = 17, address = "gfhghfg"}
};
List<student> query = student.students.Where(x => x.Name == "david").ToList();
}
}
public class student
{
public static List<student> students {get; set;}
public string Name{get;set;}
public int age{get;set;}
public string address{get;set;}
public string property {get;set;}
}
}