Linq to SQL C#-数字范围 [英] Linq to SQL C# - number range
问题描述
你好.我只是在学习c#,并且试图了解如何使用Linq查询SQL.我已经在vb中使用2个文本框,一个datagridview和一个commit按钮制作了一个应用程序,想知道用户输入一个最小数字(不能小于0)来从db中选择一行的代码是什么.一个文本框,然后在另一个文本框中输入一个最大数字(不能大于5).用户点击了Submit,它应该显示有效的行,其中包含该列的值介于他们在文本框中输入的2个数字之间.
这是我到目前为止所拥有的
我有一个文本框和一个搜索按钮,用于按姓氏搜索数据库.很好用.
我希望能够输入2个数字,然后单击我拥有的另一个搜索按钮(btnAverage)并获取平均值.
Hello there. I am just learning c# and I''m trying to understand how to query SQL with Linq. I have made an application in vb using 2 textboxes, a datagridview and a submit button and would like to know what the code is to select a row from a db by a user inputting a min number(which can be no lower than 0) into one textbox and inputting a max number(which can be no higher than 5) into another textbox. The user hits submit and it should show the valid rows contain the column that has a value in between the 2 numbers they input in the textboxes.
this is what I have so far
I have a textbox and a search button that searches the db by lastname. that functions just fine.
I would like to be able to enter the 2 numbers and click on the other search button I have(btnAverage) and get the average number.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MasterDetailProject
{
public partial class Details : Form
{
//ATTRIBUTES
private BaseballDataContext database = new BaseballDataContext();
private const double MIN_AVG = 0.000;
private const double MAX_AVG = 1.000;
//CONSTRUCTOR
public Details()
{
InitializeComponent();
}//end constrcutor
private void Details_Load(object sender, EventArgs e)
{
txtLastName.Text = "Name";
}//end Details_Load
//METHODS
private void RefreshContacts()
{
//show everything in db, sort by lastname, firstname - SHOWS EVERYONE IN AddressBook. just call whenever need be
playerBindingSource.DataSource =
from address in database.Players
orderby address.LastName, address.FirstName
select address;
//go to the first result
playerBindingSource.MoveFirst();
//clear the find textbox
txtLastName.Clear();
}
//EVENT HANDLER
private void btnSearch_Click(object sender, EventArgs e)
{
playerBindingSource.DataSource =
from PlayerID in database.Players
where PlayerID.LastName.StartsWith(txtLastName.Text)
orderby PlayerID.LastName, PlayerID.FirstName
select PlayerID;
playerBindingSource.MoveFirst();
}//end btnSearch_Click
private void btnAverage_Click(object sender, EventArgs e)
{
playerBindingSource.DataSource =
from PlayerID in database.Players
where PlayerID.BattingAverage.
}//end btnAverage
}//end class}//end namespace
有什么想法吗?
Any ideas?
推荐答案
您是说battingaverage值应该在min_avg和max_avg之间吗?像这样 ? :
do you mean that the battingaverage value should be between min_avg and max_avg ??? like this ? :
playerBindingSource.DataSource =
from PlayerID in database.Players
where PlayerID.BattingAverage > MIN_AVG & PlayerID.BattingAverage < MAX_AVG
select PlayerID;
这篇关于Linq to SQL C#-数字范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!