2012年9月17日 星期一

{Linq} 多個OrderBy 條件

var sum = pSumDollar.OrderBy(s => s.No).ThenBy(s => s.Date);

最先排序Order By
之後每一次的排序用ThenBy

ex:
原始資料>>
No   Date
1      2012/09/17
2      2012/09/18
1      2012/06/19
3      2012/07/20

第一次 Order By

No   Date
1      2012/09/17
1      2012/06/19
2      2012/09/18
3      2012/07/20

第二次

No   Date
1      2012/06/19
1      2012/09/17
2      2012/09/18
4      2012/07/20


參考資料:
http://www.cnblogs.com/meil/archive/2011/12/08/2281054.html

{C#} 對數值四捨五入


Math.Round() ,Decimal.Round() 這兩個函式>>前一位為奇數則進位;反之則不進位(就算大於5,也不進位)
Decimal.Round(1.45,1)>>1.4
Decimal.Round(1.55,1)>>1.6


實現四捨五入:
1.無條件進位=>Math.Ceiling()  ;  無條件捨去=>Math.Floor()
        四捨五入
        dNum = 12.5;
        dNum = Math.Floor(dNum+0.5);

       取小數後兩位
       dNum = 0.1312141231;
       dNum = Math.Floor(dNum*100+0.5) / 100;

2.利用ToString()
       double dd = 300.56;
       dd.ToString("0"); --> 結果是 301
       dd.ToString("0.0"); --> 結果是 300.6

3.Decimal.Round參數
(數值,取小數幾位, 不管小數第二位為奇或偶數值,第三位過五(含五)就進位)
decimal.Round(Quota, 2, MidpointRounding.AwayFromZero)





參考來源:
http://blog.yam.com/terrytg259/article/26728510
http://msdn.microsoft.com/zh-tw/library/6be1edhb%28v=vs.80%29.aspx
http://msdn.microsoft.com/zh-tw/library/system.midpointrounding(v=vs.80).aspx

{SQL,Linq} 取前幾個月的資料寫法

SQL:
--本月最後一天
   SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))


--前二個月的資料 ex:now為九月 則取出七月與八月的資料
    SELECT *   FROM  tableName
  WHERE   Date       between   DATEADD(month,   -2,   GetDate())     and  DATEADD(month,   0,   GetDate())


Linq:
--指定區間內資料
               var  invoiceData = (from item in table
                               where item.Date >= startDT && item.Date <= endDT
                               select new
                               {
                                   Id = item.Id,
                                   Date = item.Date
                               });
參考資訊:
http://topic.csdn.net/t/20040920/18/3391864.html
http://topic.csdn.net/u/20110622/10/11c96822-edf8-4f7b-8cf5-49a8354f6edc.html
http://www.itpub.net/thread-232401-1-1.html
http://blog.sina.com.cn/s/blog_5067ef490100lg49.html
http://horacerobot.blogspot.tw/2010/10/linq-datetime-between.html

{C#} Gridview 欄位合並


//結果圖:

//針對前三欄如上一欄資料與下一欄資料相同,則合並
protected void MainDG_PreRender(object sender, EventArgs e)
        {
                //分格Row
                int i = 1;
                foreach (GridViewRow item in MainDG.Rows)
                {
                    if (item.RowIndex != 0)
                    {
                        if (item.Cells[0].Text.Trim() == MainDG.Rows[item.RowIndex - i].Cells[0].Text.Trim() && item.Cells[1].Text.Trim() == MainDG.Rows[item.RowIndex - i].Cells[1].Text.Trim()
                            && item.Cells[2].Text.Trim() == MainDG.Rows[item.RowIndex - i].Cells[2].Text.Trim() && item.Cells[3].Text.Trim() == MainDG.Rows[item.RowIndex - i].Cells[3].Text.Trim())
                        {
                            MainDG.Rows[(item.RowIndex - i)].Cells[0].RowSpan += 1;
                            item.Cells[0].Visible = false;
                            MainDG.Rows[(item.RowIndex - i)].Cells[1].RowSpan += 1;
                            item.Cells[1].Visible = false;
                            MainDG.Rows[(item.RowIndex - i)].Cells[2].RowSpan += 1;
                            item.Cells[2].Visible = false;
                            MainDG.Rows[(item.RowIndex - i)].Cells[3].RowSpan += 1;
                            item.Cells[3].Visible = false;
                            i++;
                        }
                        else
                        {
                            MainDG.Rows[(item.RowIndex)].Cells[0].RowSpan += 1;
                            MainDG.Rows[(item.RowIndex)].Cells[1].RowSpan += 1;
                            MainDG.Rows[(item.RowIndex)].Cells[2].RowSpan += 1;
                            MainDG.Rows[(item.RowIndex)].Cells[3].RowSpan += 1;
                            i = 1;
                        }
                    }
                    else
                    {
                        item.Cells[0].RowSpan = 1;
                        item.Cells[1].RowSpan = 1;
                        item.Cells[2].RowSpan = 1;
                        item.Cells[3].RowSpan = 1;
                    }
                }
           
        }


參考資料:
http://www.dotblogs.com.tw/mis2000lab/archive/2008/04/24/3451.aspx
http://epaper.blueshop.com.tw/board/show.asp?subcde=BRD20061225110536B4G&fumcde=FUM20050124192253INM
http://pramaire.pixnet.net/blog/post/28100706-gridview%E6%A8%99%E9%A1%8C%28%E8%A1%A8%E9%A0%AD%29%E5%90%88%E4%BD%B5

{C#} C#中讀取JavaScript 方法

1.Response.Write("JavaScript的字串")
>>執行後Script語法將跑至最上頭,以致產生「找不到物件」的問題


2.RegisterClientScriptBlock('script','JavaScript的字串')
>>執行後,置於<body>區塊 最頂端

3.RegisterClientScriptInclud('script','JavaScript的字串')
>>執行後,置於<head>裡

4.RegisterStartupScript ('script','JavaScript的字串')
>>執行後,置於<body>區塊最末端, 所有的control重新Load一次後才執行

5.把JavaScript 加在控制項上
>>使用 Ex:
button1.Attributes.Add('onclick', JS FunctionName);


http://dnowba.blogspot.tw/2012/07/aspnetjavascript_12.html

多種使用方式:
http://irismyself.pixnet.net/blog/post/12388073-%E3%80%90asp.net%E3%80%91%E3%80%90javascript%E3%80%91%E5%9C%A8asp.net-%E5%91%BC%E5%8F%AB-javascript-




Page.ClientScript.RegisterClientScriptBlock
Page.ClientScript.RegisterStartupScript
Page.ClientScript.RegisterClientScriptInclude

http://welkingunther.pixnet.net/blog/post/27831277-(asp.net)%E5%8B%95%E6%85%8B%E8%A8%BB%E5%86%8Ajavascript%E8%87%B3client%E7%AB%AF

{Linq}將Linq結果轉為dt

當把Datatable的資料做Linq的查詢處理後
轉為一Datatable

CopyToDataTable()只支援Datarow
可加擴充功能
 http://msdn.microsoft.com/en-us/library/bb669096.aspx

http://bibby.be/2008/05/anonymous-typeclass.html
http://saam0813.wordpress.com/2010/06/08/linq-copytodatatable-of-t/
http://saam0813.wordpress.com/2010/06/08/linq-copytodatatable-of-t/

{SQL} SQL DECIMAL Type Use

DECIMAL(m,n)>>整數值+小數值=m

參考資料:
http://www.cppblog.com/prayer/archive/2009/05/04/81825.html

{C#} log4net Use

//引入.dll與xml

//宣告
static log4net.ILog logger =
log4net.LogManager.GetLogger(System.Reflection.MethodBase
.GetCurrentMethod().DeclaringType);

code:
loger.ErrorFormat("{0} {1}", a, ex.b);




參考資料:
http://luckystar1216.pixnet.net/blog/post/13159723-%E4%BD%BF%E7%94%A8log4net%E7%B4%80%E9%8C%84%E7%A8%8B%E5%BC%8F%E5%9F%B7%E8%A1%8C%E9%81%8E%E7%A8%8B%E7%9A%84log%28for-c%23%29