包含Group Join的VB.NET Linq代码转C#时第二from语法报错求助
正确转换代码
var query = (from UpdateItemBase egyosztaly in UpdateItemCollectionRemote join UpdateItemBase egystudent in UpdateItemCollectionLocal on egyosztaly.HashCode equals egystudent.HashCode into csoport from egycsoportelem in csoport.DefaultIfEmpty() select new ComparedUpdateItem { Remote = egyosztaly, Local = egycsoportelem }).ToList();
核心修正点:
- 显式声明了两个集合的元素类型为
UpdateItemBase:如果你的UpdateItemCollectionRemote、UpdateItemCollectionLocal属于非泛型集合(仅实现IEnumerable接口而非泛型IEnumerable<UpdateItemBase>),C#无法自动推断元素类型,必须显式声明,否则会报语法错误。 - 补全了
DefaultIfEmpty的方法调用括号:VB支持无参数方法省略括号的语法,C#不支持,缺失括号会被识别为访问不存在的属性,直接触发语法错误。
额外检查项
如果调整后仍有报错,可排查以下两点:
- 确认
ComparedUpdateItem的Remote、Local属性为公开可写,支持对象初始化器赋值 - 确认两个集合的元素类型都继承自
UpdateItemBase,且HashCode属性类型一致,可直接判等
内容的提问来源于stack exchange,提问作者SZL
相关产品推荐
相关产品推荐

