標籤:out using ash control get max 最大值 int 欄位
一、LinQ Distinct某欄位去重
建立類GoodsIdComparer,繼承 IEqualityComparer<Goods>,實現Equals方法
public class GoodsIdComparer : IEqualityComparer<Goods> { public bool Equals(Goods x, Goods y) { if (x == null) return y == null; return x.Gproducer == y.Gproducer; } public int GetHashCode(Goods obj) { if (obj == null) return 0; return obj.Gproducer.GetHashCode(); } }//根據產地(Gproducer)去重
使用的時候,只需要
var distinctGoods= allGoods.Distinct(new GoodsIdComparer());//需要引用命名空間
二、MVC的控制器Controllers中用using直接調用資料庫組合查詢
在MVC的控制器Controllers中用using直接調用資料庫組合查詢,返回視圖時應注意為:
return View(new List<Goods>(All));
using (FruitDataContext con = new FruitDataContext()) { var All = con.Goods.AsEnumerable(); if (category != "") { var Category = con.Goods.Where(r => r.Gcategory == category); All = All.Intersect(Category); } } return View(new List<Goods>(All));
視圖中引用強型別:
@model List<Goods>
三、LinQ查詢資料庫中自增列ID的最大值
public int Maxid() { return con.Goods.Max(r=>r.Gids); }
網頁端程式小知識