2012-02-01 65 views
1

我有两个N:N相关的实体。随着一个例子,我会告诉你我是什么意思:CRM 2011 - N:N(多对多)Linq问题

  • 我有一个会话(ave_Session),而且我们可以把“培训师” (ave_trainer)每个会话
  • 我tryting获得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。提前Thx如果你能帮助我!

+4

而不是使用'f.id'和'c.id的自由',尝试使用'f.ave_trainerid'和'c.ave_sessionid'。 – 2012-02-01 17:08:41

+0

Peter的评论是答案(.id不是LINQ查询中Guid字段的可用简写形式)。看到这个类似的问题/答案︰https://stackoverflow.com/questions/23373931/how-to-retieve-crm-guid-using-linq-and-joins – 2017-08-22 02:58:04

回答

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); 
} 
+0

这就是我也发现..上述加入是基于那..但是我似乎没有找到正确的方式来做到这一点......我首先需要从关系中做一个或? – Freeetje 2012-02-01 16:47:07

1

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

你通常会做的是先把你的'开始的东西',然后通过映射去找到你想要的东西。我没有任何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();