發表文章

目前顯示的是有「c#」標籤的文章

快速將List中重覆資料排除

最近工作的關係要從不同的table來撈資料。對於Linq不太熟的我真的是一大挑戰。 以前用SQL就可以很快的將不同table組在一起同時可以做group資料。現回到Linq上來 Linq也可以做grounp資料。但是如果中間做了很多復雜的查詢與應用到~可能不太方便用group的話…雖然網路上有很多方法是用泛型加foreach的方式去比較,但是操作上可能還是有點復雜與要再加工一下…這個時候可以用下列的方法做處理。 var list = new List<string> {"A", "C", "B", "D", "A", "B", "E", "D", "B", "C"};     var set = new HashSet<string>(list);  foreach (string item in set)    {      Console.WriteLine(item);     }

關於DbUpdateConcurrencyException

Building an MVC 4 App with Database First and Entity Framework 最近遇到的專案,是已經有大量的資料在資料庫裡。對於這種情況,就不適合使用 比較熟悉的code first的方式來開發,所以將採用DB First的方式來進行。當我們用 ADO.NET Entity Data Model,visual studio 將會為我們先建立好相關的類別、模 型。 在這個時候問題來了,採用這種方式的話,我們在code first中,我們會直接把 表單驗證的條件寫在model中,但是db first的方式,我們取得的model是經由範本 產生的,所以當我們資料庫有做了異動,或是我們的model有做更新的話,我們把 驗證的條件寫在自動產生的model中,將會全被清掉。 在自動產生的類別可以看到這幾行的註解。 //------------------------------------------------------------------------------ // <auto-generated> //    This code was generated from a template. // //    Manual changes to this file may cause unexpected behavior in your application. //    Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //---------------------------- 所以這時候我們將要用到Buddy Class的幫忙。 例如一開始我們建立的model如下 :  namespace MvcLeaning . Models {     using System ;     usin...

取得Master Page的物件

有時候我們在開發asp.net 的時候會使用master page,但是如果master page上有某個物件或是控製元件,我們希望他進到不同的頁面時,可以有做一些改變,或是取值,這個要怎麼做呢? 方法如下。 在Master Page的c#檔中的PageLoad裡定義屬性 /// DropdownList   public string ddl_menu {         get { return nav.MenuValue;}         set { nav.MenuValue = value; }         } 在index.aspx中 給值 this.Master.ddl_menu = "1"; 取值 String temp = this.Master.ddl_menu;

筆記Entity SQL的用法

找了滿多篇如何做到像SQL 語法中的 inner join或是Left join,但看了還是不太熟,在這邊筆記一下。讓自已不要忘記。 如果有兩個Table一個是基本資料、一個是教育程度,在基本資料中關於學位的部份,只存放的教育程度的ID,以方便日後再增加其他的項目。如果想要把資料顯示的時候不要顯示basic時,在學位的部份只有id這邊可以使用inner join的方式。 var query = from basic in context.BasicInfo                    join ed in context.Educations on basic.Education equals ed.ID                   select new{                        姓名=basic.LocalName,                        學位=ed.Name                    } gvResult.Datasource = query; gvResult.DataBind();

c# 把List中重復的資料去掉

using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { // List with duplicate elements. List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(3); list.Add(4); list.Add(4); list.Add(4); foreach (int value in list) { Console.WriteLine("Before: {0}", value); } // Get distinct elements and convert into a list again. List<int> distinct = list.Distinct().ToList(); foreach (int value in distinct) { Console.WriteLine("After: {0}", value); } } } Output Before: 1 Before: 2 Before: 3 Before: 3 Before: 4 Before: 4 Before: 4 After: 1 After: 2 After: 3 After: 4

asp.net c# 學習筆記

使用MasterPage的方法 1.在aspx的原始檔,按下編輯程式碼的頁面,到最上面加入MasterPageFile及Title <%@ Page Language="c#" AutoEventWireup="false" CodeFile="test.aspx.cs" Inherits="test" MasterPageFile="MasterPage名稱.master" Title="網頁標題名稱" %> 2.<html></html>、<head></head>、<title></title>、<body></body>標籤這些都刪除 3.在設計頁面的ContentPlaceHolder元件按右鍵→建立自訂內容 4.再將原<body></body>中的內容複製到<asp:Content...略></asp:Content>中即可 讀取get post的方法 get的方法 string id = Request["ID"] post的方法 Request.Form["變數"]

c# 學習筆記 - 將英文字串中,每個單字第一個字母變大寫

將一串英文字,每個單字第一個字母轉換成大寫 using System; using System.Collections.Generic; using System.Web; /// <summary> /// Summary description for Tools /// </summary> public class Tools { public Tools() { // // TODO: Add constructor logic here // }     public static string UppercaseWords(string value)     {         char[] array = value.ToCharArray();         // Handle the first letter in the string.         if (array.Length >= 1)         {             if (char.IsLower(array[0]))             {                 array[0] = char.ToUpper(array[0]);             }         }         // Scan through the letters, checking for spaces.         // ... Uppercase the lowercase le...