sugarcrum
sugarcrum

Reputation: 1011

LINQ to SQL and Column Alias with spaces

Still a bit new to Linq. This is driving me nuts. I want to alias a column, the alias should have a space.

This works fine:

Dim q = From tmp in t.Table Select iDate = tmp.iDate

But, I want this to work

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

Any ideas?

Upvotes: 10

Views: 22828

Answers (7)

estinamir
estinamir

Reputation: 503

If you need it for databinding to gridview this example could work:

    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>

Upvotes: 0

Kishore Kumar
Kishore Kumar

Reputation: 12884

Use Square Bracket to use alias in LinqToSql like

[Some Alias With Space] = tmp.iDate

if this wont work then remove white space and use []

Upvotes: 2

KRISHNA RAO
KRISHNA RAO

Reputation: 11

BY USING DATA TABLE

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();

Upvotes: 1

Guaroa Mendez
Guaroa Mendez

Reputation: 21

Use simple alias before the column or columns:

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

//Total is the alias

Upvotes: 2

Serapth
Serapth

Reputation: 7172

You can't, sorry. Hell, if you bring a table with a space in the name into Linq it will automatically replace the space with underscores.

Besides it not being possible, it is an extremely overwhelmingly bad idea. Something that people that wrote Access should be shot because of.

Upvotes: -1

David Yancey
David Yancey

Reputation: 2030

First of all Alias's can't have spaces, just like any variable name can't have a space. My question tho is why would you want/need to have a space in your name? I'm sure there are better means of accomplishing what your trying to achieve with out trying to institute bad practices of bad naming conventions.

Upvotes: 3

eKek0
eKek0

Reputation: 23299

As far as I know, you can't do this because columns alias must be valid C# identifiers, and they don't allow whitespaces.

Upvotes: 0

Related Questions