具有多个内部联接和单个 WHERE 子句的 CRM 2011 LINQ 查询 (VB .NET)

发布于 2024-12-02 15:38:26 字数 722 浏览 1 评论 0原文

我在尝试让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南…巷孤猫 2024-12-09 15:38:26

我没有设置 CRM 来测试这是否只是其提供商的限制,但您可以尝试以下操作:

Dim MyVar = From a In svcContext.AccountSet _
    Where a.Name.Contains("c") _
    Join c In svcContext.ContactSet On a.PrimaryContactId.Id Equals c.ContactId _ 
    Join l In svcContext.LeadSet On a.OriginatingLeadId.Id Equals l.LeadId _ 
    Select c.FullName

几点评论:与 TSQL 不同,LINQ 中的操作顺序更加灵活。这样您就可以在进行连接之前限制您的第一个表(取决于 CRM 提供商的翻译方式)。

其次,您想知道“c”在名称中的任何位置,还是在开头?如果在开始处,请考虑 Where a.Name.StartsWith("c")
另一条评论是,在您的 Select 投影中,您不必要地投影到一个可枚举类中。如果您只是投影单个值,则不需要额外的类开销。然后在 foreach 中,只需执行 Debug.Print(MyItem) 因为 MyItem 是全名。同样在VB中,使用查询语法时,不需要New With {...}。如果您想投影具有多列的匿名类型,您可以在 VB 中执行以下操作,就像在 SQL 中一样:

Dim query = From c In Customers
            Select C.FullName, C.CompanyName

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:

Dim MyVar = From a In svcContext.AccountSet _
    Where a.Name.Contains("c") _
    Join c In svcContext.ContactSet On a.PrimaryContactId.Id Equals c.ContactId _ 
    Join l In svcContext.LeadSet On a.OriginatingLeadId.Id Equals l.LeadId _ 
    Select c.FullName

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:

Dim query = From c In Customers
            Select C.FullName, C.CompanyName
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文