select、驗證

來源:互聯網
上載者:User

1)投射(select)的含義:在查詢中建立新對象 

在

var result=_db.mycontext.where(a=>a.id==id).select(new {a.id,a.name,a.createtime})foreach(var c in result){        Console.Writeline(result.);}

 

中為什麼列印出來的結果形如

{id=001,name="benjamin",createtime="20121027"}

{id=002,name="John",createtime="20121011"}

{id=003,name="Chris",createtime="20121018"}

?甚至都不需要重寫ToString()方法?

原因是編譯器面對匿名對象時已經提供了預設的執行代碼,以類似於對象初始化的方式輸出屬性的名稱和值。

2)用FirstOrDefault()--可以簡化where+select篩選

3)UpdateModel(model),TryUpdateModel(model)方法可以將傳遞過來的資料(form,querystring等)自動更新到匹配到屬性相同的model裡。

在new了一個model時,可以這樣用。

4)如何使用ModelState驗證:

1、給表單的相關屬性添加上特性如:[Required]

2、在出現錯誤資訊的位置加上

@Html.ValidationMessageFor(model => model.Name)

3、再在伺服器端用ModelState.IsValid來驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.