Click here to Skip to main content
15,885,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am writing a linq qyery to get the output, but i want some alias names for the columns. my query is like this


C#
var q = from a in Number
        join b in Dupnum on a.ID equals b.ID
                           select new
                           {
                                a.Number ,
                                a.LineNumber,
                               a.Item,
                               a.Des,
                               a.SerialNumber
                           };


I am writing like this for alias name
C#
["Number#"] = a.Number ,
[Lime Number#] =a.LineNumber

But it was throwing me the error like" Invalid anonymous type member declare, anonymous type must be declare"

Regards,
S.Inayat
Posted
Updated 16-Jan-22 6:34am
v2
Comments
Wendelius 3-Jan-12 11:13am    
Pre tags added

You should be able to simply define an alias without any extra characters as long as the names of the aliases conform to C# naming rules. For example, try the following:
C#
var q = from a in Number
        join b in Dupnum on a.ID equals b.ID
                           select new
                           {
                               f1 = a.Number ,
                               f2 = a.LineNumber,
                               f3 = a.Item,
                               f4 = a.Des,
                               f5 = a.SerialNumber
                           };
 
Share this answer
 
C#
DGV_customergrid.DataSource = from S in dc.CUSTOMERs
                                              select new
                                              {
                                                 CustomerName = S.CUSTOMERNAME,
                                                  Customer2 = S.CUSTOMER2,
                                                  FathersName = S.SO,
                                                  Address = S.RESIDENCE,
                                                  Village = S.VILLAGE,
                                                  Mandal = S.MANDAL
                                              };
 
Share this answer
 
I had the same problem and added a dot in-front of the alias. So in your code case it would be

.["Number"] = a.Number ,
.[Lime Number] =a.LineNumber



All the best,
Tony
 
Share this answer
 
Comments
vikastiwari1984 5-Jun-13 4:42am    
Solution 3 is wrong, can't provide an alias with spaces by any approach in linq.
You can also use the let operator for your query like:

var q = from a in studentList
let Number = a.Number
let LimeNumber = a.LineNumber
select new { Number, LimeNumber	};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900