發表文章

目前顯示的是 1月, 2024的文章

ASP.Net WebForm實作Open Dialog視窗互動

ASP.Net WebForm實作Open Dialog視窗互動 aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourNamespace.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>彈跳視窗範例</title> <style> #popup { display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); border: 1px solid #ccc; background-color: #fff; padding: 20px; z-index: 1000; width: 300px; /* 調整寬度 */ } #popupTitle { font-size: 18px; font-weight: bold; margin-bottom: 10px; } #popupContent { margin-bottom: 20px; } #popupButtons { text-align: right; } #popupButtons button

使用EntityFramework批次刪除資料

使用EntityFramework批次刪除資料 using (EntitiesContext db = new EntitiesContext(connString)) { // Retrieve all records from database for deletion IEnumerable<entity> entityList = db.entity.where(x => x.id == id).toList(); // Use Remove Range function to delete all records at once db.entity.RemoveRange(entityList); // Save changes db.SaveChanges(); }  

LINQ, 依筆數切分Datatable為多個Datatable

LINQ, 依筆數切分Datatable為多個Datatable using System; using System.Data; using System.Linq; class Program { static void Main() { // 假設有一個包含資料的 DataTable DataTable originalDataTable = GetOriginalDataTable(); // 指定每個小 DataTable 的大小 int batchSize = 1000; // 使用 LINQ 拆解 DataTable var smallDataTables = originalDataTable.AsEnumerable() .Select((row, index) => new { row, index }) .GroupBy(x => x.index / batchSize) .Select(group => group.Select(x => x.row) .CopyToDataTable()) .ToList(); // 打印結果,這裡只是簡單的範例,實際上你可能需要對 smallDataTables 做進一步的處理 foreach (var smallDataTable in smallDataTables) { PrintDataTable(smallDataTable); } } static DataTable GetOriginalDataTable() { // 這裡假設你有一個包含資料的 DataTable,你可以根據實際需求修改 DataTable originalDataTable = new DataTable(); // 假設有一個 "ID"

C#, Datatable, Join, LINQ, 子查詢

C#, Datatable, Join, LINQ, 子查詢 void Main() { DataTable dt1 = new DataTable(); dt1.Columns.Add("A", typeof(string)); dt1.Columns.Add("B", typeof(string)); dt1.Columns.Add("C", typeof(string)); dt1.Columns.Add("D", typeof(int)); // 資料填充 DataRow dr1 = dt1.NewRow(); dr1["A"] = "A1"; dr1["B"] = "B1"; dr1["C"] = "C1"; dr1["D"] = "1"; dt1.Rows.Add(dr1); dr1 = dt1.NewRow(); dr1["A"] = "A2"; dr1["B"] = "B2"; dr1["C"] = "C2"; dr1["D"] = "2"; dt1.Rows.Add(dr1); DataTable dt2 = new DataTable(); dt2.Columns.Add("E", typeof(string)); dt2.Columns.Add("F", typeof(string)); dt2.Columns.Add("G", typeof(int)); // 資料填充 DataRow dr2 = dt2.NewRow(); dr2["E"] = "A1"; dr2["F"] = "B1"; dr2["G"] = "10

C# 撰寫擴展CLASS - DistinctBy

C# 撰寫擴展CLASS - DistinctBy void Main() { List<ClassA> list = new List<UserQuery.ClassA>(); list.Add(new ClassA() { strA = "A", strB = "A1" }); list.Add(new ClassA() { strA = "B", strB = "B1" }); list.Add(new ClassA() { strA = "B", strB = "C1" }); var a = list.DistinctBy(x => x.strA).ToList(); // strA , strB // A, A1 // B, B1 var b = list.DistinctBy(x => x.strB).ToList(); // strA , strB // A, A1 // B, B1 // B, C1 } public class ClassA { public string strA {get;set;} public string strB {get;set;} } //撰寫一個擴展CLASS public static class DistinctByClass { //方法套用在列舉上 public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) { HashSet<TKey> seenKeys = new HashSet<TKey>(); foreach (TSource element in source) { if (seenKeys.Add(keySelector(el