Linq to sql LINQ到SQL和带空格的列别名

Linq to sql LINQ到SQL和带空格的列别名,linq-to-sql,Linq To Sql,对Linq来说还是有点新鲜。这让我快发疯了。我想给列添加别名,别名应该有一个空格 这很好: Dim q = From tmp in t.Table Select iDate = tmp.iDate 但是,我希望这能奏效 Dim q = From tmp in t.Table Select "Some Alias With Space" = tmp.iDate 有什么想法吗?据我所知,您不能这样做,因为列别名必须是有效的C标识符,并且它们不允许空白。首先,别名不能有空格,就像任何变量名都不能有

对Linq来说还是有点新鲜。这让我快发疯了。我想给列添加别名,别名应该有一个空格

这很好:

Dim q = From tmp in t.Table Select iDate = tmp.iDate
但是,我希望这能奏效

Dim q = From tmp in t.Table Select "Some Alias With Space" = tmp.iDate

有什么想法吗?

据我所知,您不能这样做,因为列别名必须是有效的C标识符,并且它们不允许空白。

首先,别名不能有空格,就像任何变量名都不能有空格一样。我的问题是为什么你想/需要在你的名字中留一个空格?我相信有更好的方法来完成你想要实现的目标,而不是试图建立不好的命名约定的坏习惯。

对不起,你做不到。该死,如果将名称中带有空格的表放入Linq,它将自动用下划线替换空格


除了不可能之外,这是一个极其糟糕的想法。写访问权限的人应该被枪杀的原因。

使用方括号在LinqToSql中使用别名,如


如果这不起作用,请删除空白并使用[]

在列之前使用简单别名:

var x = from data in mdc.Accounts
select new
{
    data.AccountName,
    Total = data.CashAndEquivalent + data.MarginBalance
};

//Total is the alias
使用数据表


如果您需要它来将数据绑定到gridview,示例可以工作:

    protected void Page_Load(object sender, EventArgs e)
    {
        using (var db = new MyDataContextDataContext())
        {
            var bl2 = (from b in db.Table1 
                          select new{b.AttendanceDate, Course = b.CourseCode + " - " + b.CourseSection, b.CourseName, TeacherName = b.StaffLastName + ", " + b.StaffFirstName}
                          );

            grd.DataSource = bl2;
            grd.DataBind();  
        }
    }

    
        protected void grd_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header){
                e.Row.Cells[0].Text = "Attendance Date";
                e.Row.Cells[1].Text = "Course Code and Section";
                e.Row.Cells[2].Text = "Course Name";
                e.Row.Cells[3].Text = "Teacher Name";
    
            }
        }
    
        <asp:GridView ID="grd" class="table table-striped" runat="server" OnRowCreated="grd_RowCreated">
            <EmptyDataTemplate>
                None
            </EmptyDataTemplate>
        </asp:GridView>

您不能在变量名称中使用空格字符。如果您使用的是t-SQL,请从Mytable[我的表名]中选择*您的别名中只能有空格。但正如david所说,代码中没有空格是没有意义的。如果您真的需要,请使用_谢谢!查找空间的原因是b/c返回的数据在IList返回q.ToList中。此时,我只是将其绑定到rad网格。网格列标题采用列名。我正在寻找一种简单的方法来处理网格上列标题中的空格。知道alias不能有空格,我将手动修改标题。感谢您的快速回复!
DataTable dt = (from x in obj.GetAllReqVSTFs()
                select new
                    {
                        VSTF_id = x.VSTF_id,
                        x.Description,
                        x.PM1,
                        x.PM2,
                        x.Analyst_Status,
                        x.Overall_Status,
                        x.Planed_Analyst_End_Date
                    }).Take(5).ToList().ToDataTable();
foreach (DataColumn c in dt.Columns)
{
    c.ColumnName = c.ColumnName.Replace("_", " ");
}

gvBurntHours.DataSource = dt;
gvBurntHours.DataBind();
    protected void Page_Load(object sender, EventArgs e)
    {
        using (var db = new MyDataContextDataContext())
        {
            var bl2 = (from b in db.Table1 
                          select new{b.AttendanceDate, Course = b.CourseCode + " - " + b.CourseSection, b.CourseName, TeacherName = b.StaffLastName + ", " + b.StaffFirstName}
                          );

            grd.DataSource = bl2;
            grd.DataBind();  
        }
    }

    
        protected void grd_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header){
                e.Row.Cells[0].Text = "Attendance Date";
                e.Row.Cells[1].Text = "Course Code and Section";
                e.Row.Cells[2].Text = "Course Name";
                e.Row.Cells[3].Text = "Teacher Name";
    
            }
        }
    
        <asp:GridView ID="grd" class="table table-striped" runat="server" OnRowCreated="grd_RowCreated">
            <EmptyDataTemplate>
                None
            </EmptyDataTemplate>
        </asp:GridView>