C# 类型动态类中不存在属性或字段

C# 类型动态类中不存在属性或字段,c#,linq,dynamic,predicate,anonymous,C#,Linq,Dynamic,Predicate,Anonymous,我正在尝试为我的报告创建一个动态查询。 我的查询返回使用.select(selectstringquery)和.where(wherestringquery)的IEnumerable。select和where字符串根据需要的内容和时间在不同情况下有所不同 string selectstringquery = "student_fname, student_lname" string wherestringquery = "student_lname='joe'"; var test = (

我正在尝试为我的报告创建一个动态查询。 我的查询返回使用
.select(selectstringquery)
.where(wherestringquery)的IEnumerable。
select和where字符串根据需要的内容和时间在不同情况下有所不同

string   selectstringquery = "student_fname, student_lname"
string wherestringquery = "student_lname='joe'";

var test = (from s in students).select(selectstringquery).where(wherestringquery); //this works well

wherestringquery = "student_isRegistered=false";
var test = (from s in students).select(selectstringquery).where(wherestringquery) //this fails coz isRegistered is a valid proerpty but not is NOT in select query. 
它抛出一个错误:不存在属性或字段“student\u isRegistered” 在“f__匿名类型337`14”类型中

但如果我在linq中放置where时它起作用

如何使其工作而不必在select查询中包含列?原因是我想在所有报表中重用查询,有些报表不需要该过滤器

此外,在本例中,我使用

“否则” 要确定wherestringquery是否为空,否则我将使用与上面完全相同的查询,但不使用。where(wherestringquery)。即使wherestringquery为空,是否有可能使用.where()?我试着写。where(true)但它抛出异常


谢谢。

在通过select子句选择较少的列后,您正在尝试应用where子句。只需在选择列之前选择where即可,例如

var test = (from s in students).where(wherestringquery).select(selectstringquery); 

您是否尝试在select?之前添加where,Something
var test=(来自学生中的s)。where(wherestringquery)。select(selectstringquery)
OK。这很有效。:)那动力在哪里呢?除了where字符串有时是空的之外,还有其他方法可以只使用1个查询吗?我不认为有什么方法可以做到这一点,一个缺点是你总是可以使用where语句,例如1=1。使用if-else也没有害处。
var test = (from s in students).where(wherestringquery).select(selectstringquery);