發表文章

目前顯示的是 10月, 2023的文章

C#, LINQ, Table Join 關聯欄位條件需加工處理

執行會發生錯誤 var query = from data in dbContextTT.AA join t2 in dbContextTT.BB on "ABC=" + data.A1 equals t2.B1 select data; 可以正常執行 var query = from data in dbContextTT.AA join t2 in dbContextTT.BB on data.A1 equals t2.B1.Replace("ABC=", "") select data;

LINQPad新增Oracle資料庫連線

圖片
 

MSSQL Over (Partition by ... Order by ...) 範例

建立Table CREATE TABLE sales ( department VARCHAR(50), employee VARCHAR(50), sale_date DATE, amount DECIMAL(10, 2) ); INSERT INTO sales VALUES ('部門A', '員工1', '2023-01-01', 100.00), ('部門A', '員工1', '2023-01-02', 150.00), ('部門A', '員工2', '2023-01-01', 200.00), ('部門B', '員工3', '2023-01-01', 50.00), ('部門B', '員工3', '2023-01-02', 75.00), ('部門B', '員工4', '2023-01-01', 300.00); 資料結構: department employee sale_date amount 部門A 員工1 2023/1/1 100 部門A 員工1 2023/1/2 150 部門A 員工2 2023/1/1 200 部門B 員工3 2023/1/1 50 部門B 員工3 2023/1/2 75 部門B 員工4 2023/1/1 300   在分區中計算行號: SELECT department, employee, sale_date, ROW_NUMBER() OVER (PARTITION BY department ORDER BY sale_date) AS row_num FROM sales; 資料結果: department employee sale_date row_num 部門A 員工1 2023/1/1 1 部門A 員工2 2023/1/1 2 部門A 員工1 2023/1/2 3 部門B 員工3 2023/1/1 1 部門B 員工4 202

C#, EF6, 建立資料庫DBEntity物件

public class MyDBEntities : DbContext { public MyDBEntities() : base("name=DBConnectionName") { } public DbSet<Table1> Table1 { get; set; } public DbSet<Table2> Table2 { get; set; } //取得每個DbSet的內容 public IQueryable<TEntity> GetALL<TEntity>() where TEntity : class { return Set<TEntity>().AsQueryable(); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //讓Entity Framework啟動不再檢查__MigrationHistory表 Database.SetInitializer<MyDBEntities>(null); base.OnModelCreating(modelBuilder); } } 使用範例 Lambda using (GeneralEntities EPEntities = new GeneralEntities()) { var IQuery_Table1 = EPEntities.GetALL<Table1>(); List<Table1> IQuery_Table1 = IQuery_Table1.Where(x => x.AA == "123").ToList(); } LINQ using (GeneralEntities EPEntities = new GeneralEntities()) { var IQuery_Table1 = from t1 in EPEntities.Table1

C#, LINQ : 取得前幾筆資料

using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List<Person> people = new List<Person> { new Person { Id = 1, Name = "約翰史密斯", Age = 35 }, new Person { Id = 2, Name = "簡道", Age = 28 }, new Person { Id = 3, Name = "約翰強生", Age = 42 }, new Person { Id = 4, Name = "愛麗絲強森", Age = 31 }, new Person { Id = 5, Name = "鮑勃史密斯", Age = 40 }, new Person { Id = 6, Name = "傑克強森", Age = 29 }, }; var query = from person in people where person.Age >= 30 && person.Name.StartsWith("約翰") orderby person.Age descending select new { Id = person.Id, Name = person.Name, Age = person.Age };

C#, LINQ : SQL子查詢

var result = from table in tables where (from subTable in tables select subTable.a).Contains(table.a) select table; 這個LINQ查詢首先從 "tables" 集合中選擇 "a" 值,然後使用 .Contains 方法來檢查主查詢中的 "table.a" 是否存在於子查詢的結果中。這相當於SQL中的 "a IN (SELECT a FROM tables)" 子查詢。

C# LINQ select group

void Main() { List<Order> orders = new List<Order> { new Order { OrderID = 1, OrderDate = DateTime.Parse("2023-09-21") }, new Order { OrderID = 2, OrderDate = DateTime.Parse("2023-09-22") }, // 添加更多订单 }; List<OrderDetail> orderDetails = new List<OrderDetail> { new OrderDetail { OrderDetailID = 1, OrderID = 1, ProductID = 101, UnitPrice = 10.0m, Quantity = 2 }, new OrderDetail { OrderDetailID = 2, OrderID = 1, ProductID = 102, UnitPrice = 15.0m, Quantity = 3 }, new OrderDetail { OrderDetailID = 3, OrderID = 2, ProductID = 103, UnitPrice = 20.0m, Quantity = 4 }, // 添加更多订单详情 }; var query = from order in orders join orderDetail in orderDetails on order.OrderID equals orderDetail.OrderID into orderGroup select new { Order = order, OrderDetails = orderGroup.ToList() }; foreach (var result in query) { Console.WriteLine($"Order ID: {result.Order.Order

C#, Entity Framework 6, 繼承DbContext

using System; using System.Data.Entity; public class MyDbContext : DbContext { public MyDbContext() : base("YourConnectionString") { // 在建構子中指定資料庫連線字串,替換 "YourConnectionString" 為你的實際連線字串 // 禁用自動遷移初始化 __MigrationHistory 表格 Database.SetInitializer<MyDbContext>(null); } public DbSet<Student> Students { get; set; } // 這是資料庫中的一個表格 protected override void OnModelCreating(DbModelBuilder modelBuilder) { // 在這裡可以進行資料庫模型的配置,例如設定索引、關聯等等 base.OnModelCreating(modelBuilder); } }

C#, EntityFramework, LINQ TransactionScopeOption.Suppress

在使用 TransactionScope進行Transaction管控時, 在其區塊所有資料的異動和資料讀取, 都會受到Lock影響, 若希望其區塊有一個不同DbContext要讀取資料, 但因Table已被別的DbContext進行資料異動而Lock時, 可以在要讀取資料的程式碼外, 包上下面TransactionScope, 他將會獨立查詢資料 using (TransactionScope tss = new TransactionScope(TransactionScopeOption.Suppress)) { } TransactionScopeOption 是一個列舉(enum)類型,用於指定 TransactionScope 的行為選項。它定義了以下四個選項: Required (預設選項): 這個選項表示當前代碼塊需要參與到已經存在的事務中。如果當前代碼塊在進入事務範圍時已經存在一個事務,則它將參與到該事務中。如果事務不存在,則會創建一個新的事務。 如果當前代碼塊成功完成,則事務將提交;如果當前代碼塊拋出異常,事務將回滾。 RequiresNew : 這個選項表示當前代碼塊需要創建一個新的獨立事務,並參與到該新事務中。無論是否存在其他事務,都將創建一個新的事務並參與其中。 當前代碼塊的操作將在新的事務內執行,並且無論成功與否,都不會影響到其他事務。 Suppress : 這個選項表示當前代碼塊將禁用事務。也就是說,當前代碼塊的操作將不受事務的約束,即使其他事務正在進行,也不會參與到任何事務中。 當前代碼塊的操作將獨立執行,不會影響到其他事務。 NotAllowed : 這個選項表示當前代碼塊不允許參與到事務中。如果當前代碼塊嘗試進入事務範圍,將引發異常。 通常用於明確禁止某些代碼塊參與到事務中的情況。 使用這些不同的選項,你可以根據你的應用程序需求來管理事務,以確保數據的一致性和可靠性。不同的選項允許你定義事務的範圍和行為方式,以適應不同的情況。