【问题标题】:CRM 2011 - N:N (Many-To-Many) Linq IssueCRM 2011 - N:N(多对多)Linq 问题
【发布时间】:2012-02-01 14:52:40
【问题描述】:

我有两个实体,它们是 N:N - 彼此相关。举个例子,我会告诉你我的意思:

  • 我有一个会话 (ave_Session),我们可以在那里放置“培训师” (ave_trainer) 在每个 Session 上
  • 我正在尝试获取所有 特定课程的“培训师”
  • 它们在 N:N(关系名称:ave_ave_session_ave_trainer)
  • 我在 VS2010 中工作并使用 C# => 我正在尝试通过 LINQ 获取数据

我最近刚开始使用 LINQ,所以也许你们可以帮助我解决这个问题。以下我已经尝试过,我给了我一个“AttributeFrom 和 AttributeTo 必须同时指定或同时省略。你不能只传递一个或另一个。AttributeFrom: , AttributeTo: ave_trainerid”-错误:

var formatteurs = (from f in ORGContext.CreateQuery<ave_trainer>()
                   join s in ORGContext.CreateQuery<ave_ave_session_ave_trainer>() on f.Id equals s.ave_trainerid.Value
                   join c in ORGContext.CreateQuery<ave_session>() on s.ave_sessionid.Value equals c.Id
                    where c.Id == item.Id
                    select f).ToList();

item.id 是会话的 ID。如果你能帮助我,请提前谢谢!

【问题讨论】:

  • 不要使用f.idc.id,尝试使用f.ave_traineridc.ave_sessionid
  • Peter 的评论就是答案(.id 不是 LINQ 查询中 Guid 字段的可行速记)。看到这个类似的问题/答案:stackoverflow.com/questions/23373931/…

标签: linq dynamics-crm-2011 crm


【解决方案1】:

来自MSDN 页面:

// List the contacts in the Softball team marketing list.
System.Console.WriteLine("List all contacts in Softball Team:");

var members = from c in crm.contacts
              join mlm in crm.listmembers on c.contactid equals mlm.entityid
              join ml in crm.lists on mlm.listid equals ml.listid
              where ml.listname == "Softball Team"
              select c;

foreach (var c in members)
{
  System.Console.WriteLine(c.fullname + " " + c.emailaddress1);
}

【讨论】:

  • 这也是我发现的.. 上面的 join 就是基于此.. 但我似乎并没有找到正确的方法......我首先需要做一个 from从关系还是?
【解决方案2】:

你现在写它的方式似乎有点倒退(假设我解析正确)。

您通常所做的是将您的“起点”放在首位,然后通过映射找到您想要的。我没有任何 CRM 2011 经验,所以希望我没有把这件事搞砸太多。 :)

另外,我不喜欢单字符名称,所以我冒昧地使用更长的名称:)

var formatteurs = (
    // first get the session we're interested in
    from session in ORGContext.CreateQuery<ave_session>()
    where session.Id == item.Id

    // now get the mapping rows that are related to it
    join mapping in ORGContext.CreateQuery<ave_ave_session_ave_trainer>() 
        on session.Id equals s.ave_sessionid.Value

    // now get from the mapping rows to the actual trainers
    join trainer in ORGContext.CreateQuery<ave_trainer>()
        on mapping.ave_trainerid.Value equals trainer.Id

    select trainer
).ToList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多