LINQ加入前1行

时间:2018-04-16 23:44:03

标签: sql linq

我想做一个只包含一行的LINQ连接。在SQL中,这就是我想要做的事情:

JOIN Person ON ( SELECT TOP (1) top1Person.ID FROM Person AS top1Person WHERE top1Person.CompanyID = Company.ID ) = Person.ID

但是我无法在LINQ中执行此操作,我尝试了这个:

join pers in ctx.Persons on cmp.Persons.First().ID equals pers.ID

但这里不允许使用Fisrt()方法......

有人可以帮助我吗?感谢

1 个答案:

答案 0 :(得分:1)

有很多方法可以实现这一目标。一种方法是在查询结果上使用First()FirstOrDefault()。试试这个:

var results = (from id in ids
               join p in persons on id equals p.ID
               where p.CompanyID == companyID
               select p).FirstOrDefault();

如果您希望获得最低ID,则可以先OrderBy(x => x.ID).FirstOrDefault();如果您想要最高ID,则可以OrderByDesc(x => x.ID).FirstOrDefault()

编辑:在进一步检查您的示例代码后,看起来这可能更像您正在寻找的内容。

var results = (from company in companies
               join p in persons on persons.First(x => x.CompanyID == company.ID).ID equals p.ID
               select p);

您应该可以在该位置调用First(),但看起来您的LINQ查询与SQL完全匹配。你应该ctx.Persons.First()而不是cmp.Persons.First()吗?如果上述解决方案对您不起作用,那么我们需要更多信息。

最终编辑:最后一个解决方案略有不同的完整解决方案。此代码经过测试并有效。

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

namespace com.test
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<int> ids = new List<int>();
            List<Company> companies = new List<Company>();
            List<Person> persons = new List<Person>();

            persons.Add(new Person(1, 1, "John Smith"));
            persons.Add(new Person(2, 1, "Adam Jones"));
            persons.Add(new Person(3, 1, "Alex Rabbit"));
            persons.Add(new Person(4, 2, "Jessica Thurman"));
            persons.Add(new Person(5, 2, "Sam Riot"));
            persons.Add(new Person(6, 2, "Donald Lewis"));
            persons.Add(new Person(7, 3, "Lindsay Bonaparte"));
            persons.Add(new Person(8, 3, "Desmond Tutu"));
            persons.Add(new Person(9, 3, "Kevin Gargoyle"));
            persons.Add(new Person(10, 4, "Emily Francis"));
            persons.Add(new Person(11, 4, "Caitlin Elizabeth"));
            persons.Add(new Person(12, 4, "Harry Finstein"));
            persons.Add(new Person(13, 4, "Carla Loper"));

            companies.Add(new Company(1, "McDonalds", persons.Where(x => x.CompanyID == 1).ToList()));
            companies.Add(new Company(2, "Burger King", persons.Where(x => x.CompanyID == 2).ToList()));
            companies.Add(new Company(3, "Wendy's", persons.Where(x => x.CompanyID == 3).ToList()));
            companies.Add(new Company(4, "Arby's", persons.Where(x => x.CompanyID == 4).ToList()));

            var results = (from cmp in companies
                           join p in persons on cmp.Persons.First().ID equals p.ID
                           select p);

            foreach (var p in results)
            {
                Console.WriteLine("Person: " + p.Name);
            }
            Console.ReadKey();
        }

        public class Person
        {
            public int ID { get; set; }
            public int CompanyID { get; set; }
            public string Name { get; set; }

            public Person (int id, int companyID, string name)
            {
                ID = id;
                CompanyID = companyID;
                Name = name;
            }
        }

        public class Company
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public List<Person> Persons { get; set; }

            public Company(int id, string name, List<Person> persons)
            {
                ID = id;
                Name = name;
                Persons = persons;
            }
        }
    }
}