发布:2023/12/7 15:54:04作者:大数据 来源:大数据 浏览次数:990
对于.xls,请使用以下内容类型
1 2 |
application/vnd.ms-excel |
对于Excel 2007版本及以上.xlsx文件格式
1 |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
我们知道使用EF Core的Join函数可以实现SQL中的INNER JOIN,那么怎么实现LEFT JOIN呢?
答案就在GroupJoin、SelectMany和DefaultIfEmpty三个Linq函数的组合使用上。
下面我们举个例子,建立一个.NET Core控制台项目,来演示使用EF Core将Person表来LEFT JOIN Products表。
Person表在EF Core中的实体类,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public partial class Person { public int Id { get; set; } public string Name { get; set; } public int? Age { get; set; } public DateTime? CreateTime { get; set; } public string Flag { get; set; } public string VarCharDescription { get; set; } } public partial class Products { public int Id { get; set; } public string Product { get; set; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Program { static void Main(string[] args) { using (TestDBContext dbContext = new TestDBContext()) { //Person LEFT JOIN Products var joinResults = dbContext .Person .GroupJoin(dbContext.Products, person => person.Id, product => product.Id, (person, products) => new { Person = person, Products = products }) .SelectMany(combination => combination.Products.DefaultIfEmpty(), (person, products) => new { PersonId = person.Person.Id, PersonName = person.Person.Name, ProductsId = products.Id, ProductsName = products.Product }).ToList(); foreach (var joinResult in joinResults) { Console.WriteLine("PersonId={0}, PersonName={1}, ProductsId={2}, ProductsName={3}", joinResult.PersonId.ToString(), joinResult.PersonName == null ? "Null" : joinResult.PersonName, joinResult.ProductsId.ToString(), joinResult.ProductsName == null ? "Null" : joinResult.ProductsName); } } Console.WriteLine("Press any key to end..."); Console.ReadKey(); } } |
© Copyright 2014 - 2025 柏港建站平台 ejk5.com. 渝ICP备16000791号-4