Skip to content

Instantly share code, notes, and snippets.

@relyky
relyky / EFCout_PageQuery.sample.cs
Last active July 6, 2021 10:42
EFCore Paging Query, PageQuery
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Your.DB;
public class YourDataService
{
// for DI injection
@relyky
relyky / JsonSerializer.cs
Last active July 5, 2024 02:41
JsonSerializer, .NET5, json, 中文字不編碼, 換行與縮排, System.Text.Json, Indent
using System;
using System.Text.Json;
var foo = new { ... };
string json = JsonSerializer.Serialize(foo, new JsonSerializerOptions {
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, // 中文字不編碼
WriteIndented = true // 換行與縮排
});
@relyky
relyky / Mapster.lab.cs
Last active June 30, 2021 03:00
Mapster, mapper, 比 AutoMapper 更好也更易使用
using Mapster;
using System;
using System.Text.Json;
//※ 這裡使用了一些 C# 9.0 的語法
namespace ConsoleApp1
{
class Program
{
@relyky
relyky / immutable_record_sample.cs
Last active June 14, 2021 08:34
C# 9.0 immutable object, 記錄類型, Record types
///
/// C# 9.0 後支援 immutable object 語法。
/// ref→[記錄類型](https://docs.microsoft.com/zh-tw/dotnet/csharp/whats-new/csharp-9#record-types)
/// ref→[Record types](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9#record-types)
/// ref→[Introducing C# 9: Records](https://morioh.com/p/8bb2b55e618d)
///
using System;
class Program
@relyky
relyky / LineBotSDK.sample.cs
Last active May 28, 2021 03:50
line.cli 指令範例
using System;
namespace LineBotConsole
{
class Program
{
static void Main(string[] args)
{
String channelAccessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx....";
String userId = "SomeOneLineId0123456789abcdef0123";
@relyky
relyky / LIFF.first.sample.jsx
Last active May 13, 2021 04:20
LINE Dev, LIFF 取得環境參數
///
/// ref→[LIFF v2 基本使用筆記及範例](https://www.letswrite.tw/liff-init/)
///
import './App.css';
import { useEffect, useState } from 'react';
import liff from '@line/liff'
const liffId = 'YOUR_REGISTERED_LIFF_ID';
@relyky
relyky / _EditItemList.cshtml
Last active April 29, 2021 10:41
MVC5, @Ajax.ActiveLink, 用 Ajax.BeginForm 取代 Ajax.ActionLink,以讓 ValidateAntiForgeryToken 有效用。
@model IEnumerable<YOUR_FORM_ATTACH>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(m => Model.FirstOrDefault().SN)
</th>
<th>
@Html.DisplayNameFor(m => Model.FirstOrDefault().AttachName)
</th>
@relyky
relyky / LinQ.SelectMany.cs
Last active April 24, 2021 03:26
LinQ, SelectMany, Union 範例
var qryDispN1 = db.消耗品登記表.Where(c => c.消耗品領用表.Count() == 0);
var qryDispN2 = db.消耗品領用表
.GroupBy(d => d.帳卡編號)
.SelectMany(g => g.OrderByDescending(d => d.序號).Take(1), (g, d) => d) // 選取分群後最新一筆明細(序號最大那筆明細)。
.Where(d => d.繳回日期.HasValue)
.Select(d => d.消耗品登記表);
qry = qryDispN1.Union(qryDispN2);
@relyky
relyky / AjaxPageSample.Controller.cs
Last active March 1, 2021 09:50
MVC5 Pagination, PagedList.Mvc, 分頁
using PagedList;
namespace YourProject.Controllers
{
public class ComputerController : Controller
{
private MineDBEntities db = new MineDBEntities();
// GET: Computer
public ActionResult Index(int? page)
@relyky
relyky / downloadFile.samle.js
Last active February 18, 2021 06:15
JavaScript, nested Promise, nest promise sample
///
/// nested Promise 應用範例源碼節段
/// React.v17 + axios
/// 功能說明:若成功下載檔案Blob,若失敗下載JSON Blob。
///
/// Do ajax download a file request
function downloadFile(apiName, args, options) {
if (!t(apiName).isString) throw new Error('Invalid value type!')
if (!(t(args).isNullOrUndefined || t(args).isObject)) throw new Error('Invalid value type!')