简体   繁体   English

在列表中创建自定义linq列名称

[英]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: 我们将一个通用List返回给GridView,然后GridView自动生成列以显示报告:

//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): 也许我误读了一些东西,但是如果你只想把你的语言放入其中(LanguagePack只是一个持有你价值观的课程):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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