当前位置: 首页 > 编程语言 > C#

Dynamic+linq编译错误分享

时间:2023-04-10 17:21:09 C#

Dynamic+linq编译错误我先声明,我在动态数据上用linq做了一些非常可怕的事情。但我无法弄清楚为什么此查询无法编译:错误1??属性'h__TransparentIdentifier0'不能与类型参数一起使用publicclass{publicstaticvoidMain(string[]args){vardocs=newdynamic[0];varq=fromdocindocsdoc["@metadata"]["Raven-Entity-Name"]=="Cases"doc.AssociatedEntities!=nullfromentitiesindoc.AssociatedEntitieswhereentity.Tags!=null//这里的编译器错误来自entity.Tags中的标签wheretag.ReferencedAggregate!=nullselectnew{tag.ReferencedAggregate.Id,doc.__document_id};}}publicstaticclassLinqOnDynamic{privatestaticIEnumerableselect(thisobjectisself){if(self==null)yieldbreakout;if(selfisIEnumerable==false||selfisstring)thrownewInvalidOperationException("Attempttoenumerate"+self.GetType().姓名);foreach(((IEnumerable)self中的变量项目)){收益率项目;}}publicstaticIEnumerableSelectMany(此对象来源,FunccollectionSelector,FuncresultSelector){returnEnumerable.SelectMany(Select(source),collectionSelector,resultSelector);}publicstaticIEnumerableSelectMany(此对象源,FunccollectionSelector,FuncresultSelector){returnEnumerable.SelectMany(Select(source),collectionSelector,resultSelector);}publicstaticIEnumerableSelectMany(thisobjectsource,Funcselector){returnSelect(source).SelectMany(selector);}publicstaticIEnumerableSelectMany(thisobjectsource,Funcselector){returnSelect(source).SelectMany(selector);为了增加侮辱伤害,以下工作:vardocs=newdynamic[0];varq=docfromdocsdoc["@metadata"]["Raven-Entity-Name"]=="Cases"doc.AssociatedEntities!=nullfromentitiesindoc.AssociatedEntitieswhereentity.Tags!=nullfromtagsinentity.Tagsselectnew{tag.ReferencedAggregate.Id,doc.__document_id};仅当我添加:wheretag.ReferencedAggregate!=null我之前有两行错误:whereentity.Tags!=null//这里的编译器错误不知道发生了什么如果我尝试将你的调用转换为:varq=fromdocindocs.Where(doc=>doc["@metadata"]["Raven-Entity-Name"]=="Cases"||doc.AssociatedEntities!=null)来自doc.AssociatedEntities.Where(entity=>entity.Tags!=null)中的实体我得到了一个不同的编译器错误,它可能会揭示正在发生的事情:'Alambdaexpressioncannotbeusedasanargumenttoadynamicaldispatchedoperationwithoutfirstconvertingittoadelegateorexpressiontreetype'所以我猜你必须重载Whereoperatorvarq=fromdocindocsdoc["@metadata"]["Raven-Entity-Name"]=="Cases"doc.AssociatedEntities!=nullfromentity((IEnumerable)doc.AssociatedEntities).Where(entity=>entity.Tags!=null)fromtag((IEnumerable)entity.Tags).Where(tag=>tag.ReferencedAggregate!=null)选择新的{tag.ReferencedAggregate.Id,doc.__document_id};这样好一点。不完美,但这就像开始-在陷入困境之前,你只能深入到这里。匿名类型返回h__TransparentIdentifier0并由编译器在编译时处理-问题显示为“动态优先顺序”-在这里阅读:C#4.0中缺少方法的方法:动态和RealProxy我今天刚刚完成这篇文章。我将进行一个小的猜测,并说匿名类型是在动态分配之后准备的:)-编译器知道这一点并让你感到沮丧。如果我使用常规类型返回,问题不会消失吗?我想它必须。以上就是C#学习教程:动态+linq编译错误分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处: