0

I'm sorry to ask this basic question,

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

I want to convert the output to string.

I tried these things:

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

and

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

both times i'm getting error:

Linq.Internals.UnoptimizedQuery<string>

Please help me out

How should i solve this problem ?

PS: If some one thinks this question is stupid or for some reason does not like it. U may delete this question after getting answered instead of flagging or down voting it

2 Answers 2

1

Try changing the query to

string filePath = (from Component comp1 in componentContainer where comp1.ComponentName == fileName select comp1.FilePath).FirstOrDefault().ToString();
Sign up to request clarification or add additional context in comments.

3 Comments

If comp1.FilePath is a string you shouldn't need to call the ToString() method.
u r my saviour!! Thanks a lot!! It worked !! But why won't it work if i don't add FirstOrDefault() ?? Can you please explain it for me ??
@user653622 The result of your query is an IEnumerable<string>. FirstOrDefault() returns the first item in the results (or null if the results are empty).
1
string filePath=componentContainer.Single(x=>x.ComponentName==fileName).FilePath;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.