public partial class 组合查询 : System.Web.UI.Page { private MyDBDataContext _Context = new MyDBDataContext(); //绑定数据车辆系列 private void FillBrand() { List<Brand> list = _Context.Brand.ToList(); ddlBrand.DataSource = list; ddlBrand.DataTextField = "Brand_Name"; ddlBrand.DataValueField = "Brand_Code"; ddlBrand.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FillBrand(); } } protected void Button1_Click(object sender, EventArgs e) { var query1 = _Context.Car.AsQueryable();//根据车名查 var query2 = _Context.Car.AsQueryable();//根据系列查 var query3 = _Context.Car.AsQueryable();//根据油耗查 //根据填写情况完善以上三个查询条件 if (txtName.Text.Trim().Length > 0) { query1 = query1.Where(p=>p.Name .Contains(txtName.Text)); } if (ddlBrand.SelectedValue != "-1") { query2 = query2.Where(p=>p.Brand == ddlBrand.SelectedValue); } if (txtOil.Text.Trim().Length > 0) { query3 = query3.Where(p=>p.Oil == Convert.ToDecimal( txtOil.Text )); } //三个查询关系可用集合的交集来实现 var query = query1.Intersect(query2).Intersect(query3); //数据绑定 显示 Repeater1.DataSource = query; Repeater1.DataBind(); } }
//点击生成流水号按钮 protected void Button2_Click(object sender, EventArgs e) { string prefix = "p" + DateTime.Now.ToString("yyyyMMdd"); //生成当天的流水号前缀 //查当天的最大流水号(a.已经有流水号 b.没有流水号) int maxFlow = 0; var query = _Context.Info.Where(p => p.Code.StartsWith(prefix)); //模糊查询当天所有的流水号 if (query.Count() > 0) //已有流水号,则找最大流水号 { query = query.OrderByDescending(p=>p.Code); //按流水号降序排 string maxCode = query.First().Code; //获取最大流水号 maxFlow = Convert.ToInt32(maxCode.Substring(maxCode.Length-3)); //截取并转换出现有的最大的流水号来 } //组合生成新的流水号 string flowCode = prefix + (maxFlow + 1).ToString("000"); //显示在文本框中 TextBox1.Text = flowCode; }