LINQ:自定义列名 [英] LINQ: custom column names
问题描述
更新
我基本上将查询绑定到WinForms DataGridView。我想要列标题是适当的,并在需要时有空格。例如,我想要一个列标题为名字,而不是 FirstName 。
I'm basically binding the query to a WinForms DataGridView. I want the column headers to be appropriate and have spaces when needed. For example, I would want a column header to be First Name instead of FirstName.
如何在LINQ中创建自己的自定义列名?
How do you create your own custom column names in LINQ?
例如:
Dim query = From u In db.Users _
Select u.FirstName AS 'First Name'
推荐答案
我解决了自己的问题,但是所有的答案都非常有帮助,并指出了我的方向。
I solved my own problem but all of your answers were very helpful and pointed me in the right direction.
在我的LINQ查询中,如果一个列名称多个单词我会用下划线分隔单词:
In my LINQ query, if a column name had more than one word I would separate the words with an underscore:
Dim query = From u In Users _
Select First_Name = u.FirstName
然后,在DataGridView的 Paint 方法中,我用一个空格替换了标题中的所有下划线:
Then, within the Paint method of the DataGridView, I replaced all underscores within the header with a space:
Private Sub DataGridView1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
For Each c As DataGridViewColumn In DataGridView1.Columns
c.HeaderText = c.HeaderText.Replace("_", " ")
Next
End Sub
这篇关于LINQ:自定义列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!