具有多个内部联接和单个 WHERE 子句的 CRM 2011 LINQ 查询 (VB .NET)
我在尝试让 LINQ 查询与 CRM 2011 一起使用时遇到了很大的困难。只要我有三个或更多联接表(似乎与哪个 CRM 表无关),并且我尝试添加 WHERE 子句(我在哪个表上过滤并不重要)。如果删除 WHERE 子句,查询将正常运行,并且可以循环访问结果集。我也可以保留 WHERE 子句,但删除第三个连接,它也可以工作。我已经对许多 CRM 实体进行了尝试,并得到了相同的错误“‘加入’操作的结果选择器必须返回两个属性的匿名类型。”似乎有一个限制,如果我想使用单个 WHERE 子句,则只能连接两个表。
我在发出 LINQ 查询时使用早期绑定 CRM 上下文生成的代码方法。这是从 CRM SDK 示例中提取的代码,但我添加了 WHERE 子句。我无法在任何地方找到如何执行此操作的示例。
Dim MyVar = From a In svcContext.AccountSet _
Join c In svcContext.ContactSet On a.PrimaryContactId.Id Equals c.ContactId _
Join l In svcContext.LeadSet On a.OriginatingLeadId.Id Equals l.LeadId _
Where a.Name.Contains("c") _
Select New With {c.FullName}
For Each MyItem In MyVar
Debug.Print(MyItem.FullName)
Next
感谢您的帮助!
I am having a great deal of difficuly trying to get LINQ queries working with CRM 2011. The problem occurs whenever I have three or more joined tables (doesn't seem to matter which CRM tables), and I try to add a WHERE clause (doesn't matter on which table I am filtering). If I remove the WHERE clause, the query runs fine and I can loop through the result set. I can also leave the WHERE clause, but remove the third join, and it also works. I have tried this with numerous CRM entities, and get the same error of "The result selector of the 'Join' operation must return an anonymous type of two properties." It appears there is a limitation that if I want to use a single WHERE clause, I am limited to joining only two tables.
I am using the early-bind CRM Context generated code method when issuing the LINQ query. This is code pulled from the CRM SDK examples, except I added the WHERE clause. I cannot find an example anywhere of how to do this.
Dim MyVar = From a In svcContext.AccountSet _
Join c In svcContext.ContactSet On a.PrimaryContactId.Id Equals c.ContactId _
Join l In svcContext.LeadSet On a.OriginatingLeadId.Id Equals l.LeadId _
Where a.Name.Contains("c") _
Select New With {c.FullName}
For Each MyItem In MyVar
Debug.Print(MyItem.FullName)
Next
Thanks for any assistance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有设置 CRM 来测试这是否只是其提供商的限制,但您可以尝试以下操作:
几点评论:与 TSQL 不同,LINQ 中的操作顺序更加灵活。这样您就可以在进行连接之前限制您的第一个表(取决于 CRM 提供商的翻译方式)。
其次,您想知道“c”在名称中的任何位置,还是在开头?如果在开始处,请考虑
Where a.Name.StartsWith("c")
另一条评论是,在您的 Select 投影中,您不必要地投影到一个可枚举类中。如果您只是投影单个值,则不需要额外的类开销。然后在 foreach 中,只需执行 Debug.Print(MyItem) 因为 MyItem 是全名。同样在VB中,使用查询语法时,不需要New With {...}。如果您想投影具有多列的匿名类型,您可以在 VB 中执行以下操作,就像在 SQL 中一样:
I don't have CRM set up to test to see if it's just a limitation of their provider, but could you try the following:
A couple comments: unlike TSQL, the order of operations in LINQ is more flexible. This way you can limit your first table before doing the join (depending on how that is translated by the CRM provider).
Second, do you want to know where "c" is anywhere in the name, or just at the start? If at the start, consider
Where a.Name.StartsWith("c")
One other comment, in your Select projection, you're needlessly projecting into an enumerable class. If you're just projecting a single value, you don't need the additional class overhead. Then in your foreach, just do Debug.Print(MyItem) because MyItem is the full name. Also in VB, when using the query syntax, you don't need the New With {...}. If you wanted to project an anonymous type with multiple columns you can do the following in VB just as you would in SQL: