栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

.NET Core 数据库查询语句

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.NET Core 数据库查询语句

微软文档 https://docs.microsoft.com/zh-cn/previous-versions/dotnet/netframework-4.0/ee358758%28v%3dvs.100%29 一.LINQ语法查询

1. 结果不要属性名以数组形式返回

var role = from e in _masterDB.EmployeeRoleAdminAcl
           join a in _masterDB.AdminAccessControlList on e.AdminAccessId equals a.AccessId
           where e.CompanyId == employee.CompanyId
           && employee.RoleIds.Contains(e.RoleId.ToString())
           && a.GroupName == "MobileReport"
           select a.AccessControl;


// 结果
[
    "EmployeeDelete",
    "AdvertisementView",
    "AdvertisementAdd",
    "AdvertisementEdit",
    "AdvertisementDelete",
    "NormalOperatingHourView",
]
二. 实体类查询

1.返回单列列表

var locs = context.Location.AsNoTracking()
.Where(l => cmps.Contains(l.CompanyId))
.Select(l => l.LocationId).Tolist();


//结果
List 类型
location1
location2
...


2.返回带名字的Group by 列表

var result = _transactionContext.H_SalesMaster.AsNoTracking()
                            .Where(s => locs.Contains(s.LocationId)
                            && s.SalesDateTime.Year >= startYear
                            && s.SalesDateTime.Year <= endYear)
                            .GroupBy(s => new { s.SalesDateTime.Year })
                            .Select(s => new { Year = s.Key.Year, total = s.Sum(t => t.TotalAmount) });
foreach (var g in result)
{
    dict.Add(g.Year.ToString(), g.total);
    list.Add(dict);
}

//结果
g.Year  g.total
三.原生SQL语句使用
//插入
int rowsAffected = context.ExecuteStoreCommand("insert Department values ({0}, {1}, {2}, {3}, {4})", DepartmentID, "Engineering", 350000.0R, "2009-09-01", 2)
        Console.WriteLine("Number of affected rows: {0}", rowsAffected)

//查询
DepartmentInfo department  = context.ExecuteStoreQuery(Of DepartmentInfo) 
                      ("select * from Department where DepartmentID= {0}",
                       DepartmentID).FirstOrDefault();


//删除
rowsAffected = context.ExecuteStoreCommand("delete from Department where DepartmentID = {0}", DepartmentID);


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/910162.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号