试图找出如何格式化我的linq语句返回的内容

时间:2014-06-27 02:27:29

标签: c# linq

我的代码不起作用,我正在试图弄清楚如何让它工作。我想格式化linq语句作为字符串返回的内容,并且该方法需要返回格式化字符串的列表。如何使我的方法有效?

public List<string> GetCurrencies()
    {
        DataClassesDataContext context = new DataClassesDataContext();

        var result = from c in context.Currencies
                     orderby c.Abbreviation ascending
                     select new { Abbreviation = c.Abbreviation, Description = c.Name };

        return result.Distinct().ToString(result.Abbreviation + " - " + result.Description).ToList();
    }

1 个答案:

答案 0 :(得分:4)

试试这个:

return context.Currencies.OrderBy(x => x.Abbreviation).Distinct().Select(x => string.Format("{0} - {1}",x.Abbreviation,x.Description)).ToList();