代码之家  ›  专栏  ›  技术社区  ›  RedBlueThing

如何在第二个表中的两个字段上进行表联接?

  •  1
  • RedBlueThing  · 技术社区  · 14 年前

    我有两张桌子:

    • 信息-除其他外,还有 托伊德 和A 弗洛伊德 字段。
    • 人-有一个对应的 个人身份证

    我想知道如何在一个单一的 林克 查询:

    给我发来的所有信息( 自我意识 )

    我在这件事上有几处裂缝。

    不完全正确

    MsgPeople = (from p in db.people
                join m in db.messages on p.person_id equals m.from_id
                where (m.from_id == idself || m.to_id == idself)
                orderby p.name descending
                select p).Distinct();
    

    这几乎奏效,但我认为它遗漏了一个案例:

    “从来没有收到过信息的人,只是给我发了一条。”

    这在我头脑中是如何运作的

    所以我真正需要的是:

    join m in db.messages on (p.people_id equals m.from_id or p.people_id equals m.to_id)
    

    给我一个我追求的人的子集

    看来你做不到。我尝试过其他一些选择,比如 执行两个连接:

    MsgPeople = (from p in db.people
                join m in db.messages on p.person_id equals m.from_id
                join m2 in db.messages on p.person_id equals m2.to_id
                where (m2.from_id == idself || m.to_id == idself)
                orderby p.name descending
                select p).Distinct();
    

    但这给了我一些我需要的结果,我想 按照联接的解析顺序进行。

    我对Linq(甚至可能是数据库理论)的理解是非常肤浅的,令人难堪,我期待能对我的问题有所了解。

    3 回复  |  直到 14 年前
        1
  •  2
  •   Amy B    14 年前

    向自己发送信息或从自己那里接收信息的人。

    from p in People
    where p.SentMessages.Any(m => m.to_id == idself)
      || p.ReceivedMessages.Any(m => m.from_id == idself)
    select p;
    

    如果您的人员没有这些邮件属性, create the associations .


    如果你想拔牙…这里是没有关联的相同查询。

    IQueryable<int> sentQuery = 
      from sent in Messages
      where sent.to_id = idself
      select sent.from_id;
    
    IQueryable<int> receivedQuery =
      from received in Messages
      where received.from_id = idself
      select received.to_id
    
    IQueryble<People> result =
      from p in people
      where System.Linq.Queryable.Concat(receivedQuery, sentQuery)
        .Any(id => id == p.personId)
      select p;
    
        2
  •  1
  •   Pharabus    14 年前

    也许我错过了一些东西,但你似乎不需要加入,你说“给我所有已经发送给和来自个人X(idself)的消息。”所以如果你只是想要消息,你可以从消息表工作,因为id(idself)是已知的。

    var MsgPeople from p in AllMessages
                       where p.from_id == idself || p.to_id == idself
                       select p;
    

    好的,试试这个,你真的需要一个给你发送消息的人的ID列表,以及一个从你那里收到消息的人的ID列表,然后你可以从这些人中选择任何存在于该列表中的人。

    var MsgPeople = from p in db.people
                            where (from m in db.messages
                                   where m.from_id == selfid
                                   select m.to_id).Union(
                            from m in db.messages
                            where m.to_id == selfid
                            select m.from_id).Contains(p.person_id)
                            select p;
    

    另一种方法是,当我向DBA提出问题时,我从我们的DBA逆向工程SQL中得到一个方法。

     var MsgPeople =  from p in Peoples
         where  p.Person_id != selfid &&
        (from m in Messages
        where m.To_id == selfid select m.From_id).Union(
        from m in Messages
        where m.From_id == selfid select m.To_id).Contains(p.Person_id)
        select p;
    
        3
  •  0
  •   Randy    14 年前

    也许试试联合吧

    select all the messages sent by user received by me
    union 
    select all the messages received by user sent by me