简体   繁体   中英

Creating custom linq column names in list

We are returning a generic List to a GridView, which then auto generates columns to show a report:

//Generate List
List<Stock> allStock = blStock_Collection.getAll();

//export custom view for report datagrid
return (from myStock in allStock
        select new
        {
            myStock.Category,
            myStock.Description,
            myLowStock.UnitPrice,
            myLowStock.CurrentQuantity
        });

Our client has asked that we now provide multi-lingual support on our site (English & Polish), specifically the column headers on grids. We would therefore need to get rid of the auto generate option on all our data grids, and add in the translations manually for each column.

I was wondering is there a way to do this when generating the datasource, which would save us a hell of a lot of time, eg changing this line:

myStock.Category,

to something like:

languagePack.Category = myStock.Category,

This is of course throwing a 'Invalid anonymous type member declarator' error. Any advice?

Maybe I misread something, but if you just want to put your language into it (LanguagePack is just a class holding your values):

class LanguagePack 
{
    public int Category { get; set; }
    public string Description { get; set; }
    public decimal UnitPrice { get; set; }
    public int CurrentQuantity { get; set; }
    public int LanguageName { get; set; }

}

return (from myStock in allStock
    select new LanguagePack
    {
        Category = myStock.Category,
        Description = myStock.Description,
        UnitPrice = myLowStock.UnitPrice,
        CurrentQuantity = myLowStock.CurrentQuantity,
        LanguageName = "Polish" // or english or whatever... maybe LanguageId or something corresponding to your model
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM