LINQ:自定义列名 [英] LINQ: custom column names

查看:128
本文介绍了LINQ:自定义列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

我基本上绑定查询一个WinForms DataGridView的。我想列标题是适当的,必要时有空格。例如,我想列标题为名字代替的


你如何创建LINQ自己的自定义列名?

例如:

 昏暗的查询=从u在db.Users _
            选择u.FirstName AS'名'


解决方案

我解决我自己的问题,但所有的答案都非常有帮助,并指出我朝着正确的方向。

在我的LINQ查询,如果列名有多个单词我会用下划线分开的话:

 昏暗的查询=从u在用户_
            SELECT名字= u.FirstName

然后,在油漆中 DataGridView的方法,我换成了空间的标题中的所有下划线:

 私人小组DataGridView1_Paint(BYVAL发件人为对象,BYVAL E上System.Windows.Forms.PaintEventArgs)处理DataGridView1.Paint
    对于每个C作为的DataGridViewColumn在DataGridView1.Columns
        c.HeaderText = c.HeaderText.Replace(_,)
    下一个
结束小组

UPDATE

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.


How do you create your own custom column names in LINQ?

For example:

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.

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆