linq或lambda语法中的共同朋友的等效查询

时间:2019-07-06 23:35:36

标签: c# sql-server linq lambda

我有两个表tblFriends和tblUsers,用于存储用户ID和朋友ID。 现在,我想在表'tblFriends'中找到user1和user2的共同朋友,以及它们的详细信息,例如昵称,年龄...

tblUsers:

Username  nvarchar
Avatar    nvarchar
Age       int


tblFriends:

IdUser1         nvarchar
IdUser2         nvarchar
FriendStatus    int

我在sql中找到了波纹管解决方案,它可以正常工作,但我需要在LINQ或Lambda中使用此查询的等效方法

我找到的解决方案在这里(https://www.codeproject.com/Questions/280296/query-to-display-mutual-friends

SELECT P1.Name
FROM dbo.Friendship AS F1
JOIN dbo.Person AS P1 ON P1.ID = F1.FriendID
WHERE F1.PersonID = 1 AND
      F1.FriendID IN (SELECT F2.FriendID
                      FROM dbo.Friendship AS F2
                      WHERE F2.PersonID = 2)

2 个答案:

答案 0 :(得分:0)

此示例等效于给定的SQL查询

using System;
using System.Collections.Generic;
using System.Linq;

namespace Friends
{
    class Program
    {
        static void Main(string[] args)
        {

            var data = new GenerateExampleData();
            var lstFriendId = (from f in data.lstFriend
                               where f.PersonId == 2
                               select f.FriendId
                              );

            var lstMutualFriend = (from f in data.lstFriend
                                   join p in data.lstPerson on f.FriendId equals p.Id
                        where lstFriendId.Contains(f.FriendId) && f.PersonId == 1
                        select p.Name
                       );
            foreach (var item in lstMutualFriend)
            {
                Console.WriteLine(item);
            }           
            Console.ReadLine();
        }
    }

    public class GenerateExampleData
    {
        public List<Person> lstPerson;
        public List<Friendship> lstFriend;

        public GenerateExampleData()
        {
            lstPerson = new List<Person>
            {
                new Person
                {
                    Id = 1,
                    Name ="Person1"
                },
                new Person
                {
                    Id = 2,
                    Name ="Person2"
                },
                new Person
                {
                    Id = 3,
                    Name ="Person3"
                },
                new Person
                {
                    Id = 4,
                    Name ="Person4"
                },

            };

            lstFriend = new List<Friendship>
            {
                new Friendship
                {
                    PersonId = 1,
                    FriendId = 2
                },

                new Friendship
                {
                    PersonId = 1,
                    FriendId = 4
                },

                new Friendship
                {
                    PersonId = 2,
                    FriendId = 4
                },
            };
        }

    }

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Friendship
    {
        public int PersonId { get; set; }
        public int FriendId { get; set; }
    }

}

答案 1 :(得分:0)

不需要 settingsArray[settingsArray.size()]

operator[]

请注意,仅当友谊关系是一种单向关系时才有效(如果user1是与user2的朋友,那么user2不一定是与user1的朋友)。

如果关系是两种方式,则有两个选择:

  1. 在DB中将关系定义为两行(每行描述一种方式)。
  2. 该关系是隐式的双向关系,因此您应该重构上面的LINQ查询:
Join