LinQ 组合查询与分页

时间:2021-12-27 23:22:01

 

1.以开头查

public List<Car> Select1(string a){

return con.Car.Where(r => r.Name.StartsWith(a)).ToList();
}

 

2.以结尾查

public List<Car> Select2(string a)

{
return con.Car.Where(r => r.Name.EndsWith(a)).ToList();
}

 

3.最大值

public string Max()

{
return con.Car.Max(r => r.Price).ToString();
}

 

4.最小值

public string Min()

{
return con.Car.Min(r => r.Price).ToString();
}

 

5.总和

 

public string Sum()
{
return con.Car.Sum(r => r.Price).ToString();
}

 

6.平均值

public string avg()
{
return con.Car.Average(r => r.Price).ToString();
}

7.升序

 

public List<Car> ss()
{
return con.Car.OrderBy( r =>r.Price).ToList();
}

 

8.降序

 

public List<Car> s()
{
return con.Car.OrderByDescending(r => r.Price).ToList();
}

9.组合分页查询

 

    public List<Car> Selecta(int d, string a, string b, string c)
{
List
<Car> list = new List<Car>();
list
= con.Car.ToList();
if (a != "")
{
List
<Car> list1 = con.Car.Where(r => r.Code.Contains(a)).ToList();

list
= list.Intersect(list1).ToList();
}
if (b != "")
{
List
<Car> list1 = con.Car.Where(r => r.Name.Contains(b)).ToList();

list
= list.Intersect(list1).ToList();
}
if (c != "")
{
List
<Car> list1 = con.Car.Where(r => r.Brand.Contains(c)).ToList();

list
= list.Intersect(list1).ToList();
}
return list.Skip((d-1-1) * PageCount).Take(PageCount).ToList();

}

int PageCount = 6;
public List<Car> start()
{
return con.Car.Skip(0 * PageCount).Take(PageCount).ToList();
}
public List<Car> prev(string a)
{
return con.Car.Skip((Convert.ToInt32(a) - 1 - 1) * PageCount).Take(PageCount).ToList();
}
public List<Car> next(string a)
{
return con.Car.Skip(Convert.ToInt32(a) * PageCount).Take(PageCount).ToList();
}
public List<Car> end()
{
return con.Car.Skip((max() - 1) * PageCount).Take(PageCount).ToList();
}

private int max()
{
int count = new CarData().Select().Count;

double aa = count / (PageCount * 1.0);

return Convert.ToInt32(Math.Ceiling(aa));
}
}
 void LinkButton4_Click(object sender, EventArgs e)
{
Repeater1.DataSource
= new CarData().end();
Repeater1.DataBind();
Label2.Text
= max().ToString();
}

private int max()
{
int count = new CarData().Select().Count;

double aa = count / (PageCount * 1.0);

return Convert.ToInt32(Math.Ceiling(aa));
}
void LinkButton3_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(Label2.Text) < max())
{
int a = Convert.ToInt32(Label2.Text) + 1;
Repeater1.DataSource
= new CarData().next(Label2.Text);
Repeater1.DataBind();
Label2.Text
= a.ToString();
}
else
{
return;
}

}

void LinkButton2_Click(object sender, EventArgs e)
{

if (Convert.ToInt32(Label2.Text) > 1)
{
int a = Convert.ToInt32(Label2.Text) - 1;
Repeater1.DataSource
= new CarData().prev(Label2.Text);
Repeater1.DataBind();
Label2.Text
= a.ToString();
}
else
{
return;
}
}

void LinkButton1_Click(object sender, EventArgs e)//首页
{
Repeater1.DataSource
= new CarData().start();
Repeater1.DataBind();
Label2.Text
= "1";
}

void Button13_Click(object sender, EventArgs e)
{
Label2.Text
= "1";
int a = Convert.ToInt32(Label2.Text) + 1;
Repeater1.DataSource
= new CarData().Selecta(a,TextBox3.Text, TextBox4.Text, TextBox5.Text);
Repeater1.DataBind();

int count = new CarData().Select().Count;


Label3.Text
= Math.Ceiling(Convert.ToDouble(count) / PageCount).ToString();


}