将输出从linq查询转换为字符串的问题

时间:2011-04-18 16:12:00

标签: asp.net linq

我很遗憾地问这个基本问题,

var filePath = (from Component comp1 in componentContainer where comp1.ComponentName == fileName select comp1.FilePath);

我想将输出转换为字符串。

我试过这些东西:

string filePath = (from Component comp1 in componentContainer where comp1.ComponentName == fileName select comp1.FilePath).ToString();

 var filePath = (from Component comp1 in componentContainer where comp1.ComponentName == fileName select comp1.FilePath);
        string filePathInString = filePath.ToString();

两次我都收到错误:

Linq.Internals.UnoptimizedQuery<string>

请帮帮我

我该如何解决这个问题?

PS:如果有人认为这个问题是愚蠢的,或者由于某种原因不喜欢它。你可以在得到答案后删除这个问题,而不是标记或缩小投票

2 个答案:

答案 0 :(得分:1)

尝试将查询更改为

string filePath = (from Component comp1 in componentContainer where comp1.ComponentName == fileName select comp1.FilePath).FirstOrDefault().ToString();

答案 1 :(得分:1)

string filePath=componentContainer.Single(x=>x.ComponentName==fileName).FilePath;