如何使用 Linq to Sql C# 填充 ListBox
How to Populate ListBox with Linq to Sql C#
我有一个问题,如何在没有 db 列和大括号的情况下使用数据库中的数据填充列表框
这是我的代码:
var db = new DataClasses1DataContext();
var history =( from a in db.ResultsHistories
where
a.PlayerOne == _playerOne && a.PlayerTwo == comboBox.Text ||
a.PlayerTwo == _playerOne && a.PlayerOne == comboBox.Text
select new
{
a.PlayerOne,
a.PlayerTwo,
a.Date,
a.ResultOne,
a.ResultTwo
}).ToList();
listBox.ItemsSource = history;
结果:
我只需要这些列中的数据。
请看这里的答案:
Multicolumn ListBox in WPF
您也可以为此目的使用 DataGrid。
XAML 代码为:
<DataGrid x:Name="NamesDataGrid"></DataGrid>
后面还有一个非常简单的代码:
public partial class MainWindow : Window
{
private readonly List<Name> _nameList = new List<Name>
{
new Name("Eric","Clapton"),
new Name("Mark", "Knopfler")
};
public MainWindow()
{
InitializeComponent();
NamesDataGrid.ItemsSource = _nameList;
}
}
internal class Name
{
public Name(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
看起来像这样:
如果您尝试 return 一个新对象,您必须将属性告诉它
select new {PlayerOne = a.PlayerOne
//More Properties Here
}
所以根据你的例子可能看起来像
var db = new DataClasses1DataContext();
var history =( from a in db.ResultsHistories
where
a.PlayerOne == _playerOne && a.PlayerTwo == comboBox.Text ||
a.PlayerTwo == _playerOne && a.PlayerOne == comboBox.Text
select new
{
PlayerOne = a.PlayerOne,
PlayerTwo = a.PlayerTwo,
Date = a.Date,
ResultOne = a.ResultOne,
ResultTwo = a.ResultTwo
}).ToList();
listBox.ItemsSource = history;
这将 return 一个没有类型但属性的新对象列表
编辑:
如果您想要来自 table 的所有数据,其中 PlayerOne 和 PlayerTwo 等于文本框值 ,只需执行
var history = db.ResultsHistories.Where(x => x.PlayerTwo == comboBox.Text || x.PlayerOne == comboBox.Text).Select(x => x).ToList();
尝试添加这个
", " + 语句之间
a.PlayerOne+”、“+a.PlayerTwo+”、“+a.Date+”、“+a.ResultOne+”、“+a.ResultTwo
它会起作用
我有一个问题,如何在没有 db 列和大括号的情况下使用数据库中的数据填充列表框 这是我的代码:
var db = new DataClasses1DataContext();
var history =( from a in db.ResultsHistories
where
a.PlayerOne == _playerOne && a.PlayerTwo == comboBox.Text ||
a.PlayerTwo == _playerOne && a.PlayerOne == comboBox.Text
select new
{
a.PlayerOne,
a.PlayerTwo,
a.Date,
a.ResultOne,
a.ResultTwo
}).ToList();
listBox.ItemsSource = history;
结果:
请看这里的答案: Multicolumn ListBox in WPF
您也可以为此目的使用 DataGrid。 XAML 代码为:
<DataGrid x:Name="NamesDataGrid"></DataGrid>
后面还有一个非常简单的代码:
public partial class MainWindow : Window
{
private readonly List<Name> _nameList = new List<Name>
{
new Name("Eric","Clapton"),
new Name("Mark", "Knopfler")
};
public MainWindow()
{
InitializeComponent();
NamesDataGrid.ItemsSource = _nameList;
}
}
internal class Name
{
public Name(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
看起来像这样:
如果您尝试 return 一个新对象,您必须将属性告诉它
select new {PlayerOne = a.PlayerOne
//More Properties Here
}
所以根据你的例子可能看起来像
var db = new DataClasses1DataContext();
var history =( from a in db.ResultsHistories
where
a.PlayerOne == _playerOne && a.PlayerTwo == comboBox.Text ||
a.PlayerTwo == _playerOne && a.PlayerOne == comboBox.Text
select new
{
PlayerOne = a.PlayerOne,
PlayerTwo = a.PlayerTwo,
Date = a.Date,
ResultOne = a.ResultOne,
ResultTwo = a.ResultTwo
}).ToList();
listBox.ItemsSource = history;
这将 return 一个没有类型但属性的新对象列表
编辑: 如果您想要来自 table 的所有数据,其中 PlayerOne 和 PlayerTwo 等于文本框值 ,只需执行
var history = db.ResultsHistories.Where(x => x.PlayerTwo == comboBox.Text || x.PlayerOne == comboBox.Text).Select(x => x).ToList();
尝试添加这个
", " + 语句之间
a.PlayerOne+”、“+a.PlayerTwo+”、“+a.Date+”、“+a.ResultOne+”、“+a.ResultTwo
它会起作用