Lỗi cannot implicitly convert type system.linq to string năm 2024

I am trying to filter one line in a text that meets the search criteria. string item = items.Where(i => i.Contains("lemon")).ToList(); I am using LINQ, and do get the following error error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List' to 'string' Why do I get this error and how can I solve this ? https://code.sololearn.com/c35n2pEOjUPX/?ref=app

1st Nov 2022, 1:43 PM

sneeze

Lỗi cannot implicitly convert type system.linq to string năm 2024

Your query returns a list of string, you won't be able to assign it to a string. Try using LINQ's First method instead: string item = items.First(i => i.Contains("lemon")); I hope this helps :) (oops I misread the question, I thought you needed one line that matched)

1st Nov 2022, 2:11 PM

Apollo-Roboto

Lỗi cannot implicitly convert type system.linq to string năm 2024

You cant assign list to string.. So string item = .. for list Is error. Use list instead List item = items.Where(i => i.Contains("lemon")).ToList();

1st Nov 2022, 2:12 PM

Jayakrishna 🇮🇳

Yes, thank you. I do understand it. I changed my code. With First I can get the first occurance With Where and String.Join, I can get all occurances if I want to and make a concatenated string.

You have to understand code is the tools forum member use to communicate. If the code is unclear, incomplete, and does not follow standard coding patterns then we cannot communicate efficiently. This is why I recommend learning MVC fundamentals and MVC standard coding patterns. We can speak the same coding language if you follow standards.

I tried my best to create an example based on your code. Unfortunately, your code is incomplete, unclear, does not follow standard coding patterns. The flow does not make a lot of sense or there are bits missing which would clarify the intent. Therefore the code example below which is based on your code technically works but is nonsensical.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Error CS0266: Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

I am fetching the employee name from DB and displaying it in a label,if the name exists it has to show the name if not it has tell that no record exists.

var hdd = from d in db.EMPs where d.Name == "JOHN" select d.City;

Label1.Text = hdd;

But i am getting an error Cannot implicitly convert type 'System.Linq.IQueryable' to 'string'

Updated 27-Jan-14 17:31pm


You might want to read the error message. It's telling you EXACTLY what's wrong. You're trying to assign an object that is not a string to a property (Text) that requires a string.

Your code doesn't make any sense since the LINQ query returns an IQueryable object (0 or more results can be returned) and you really can't convert that to a string.

This is much easier if you do not use 'Single:

var hdd = (from d in db.EMPs

         where d.Name == "JOHN"  
           select d).ToList();
Label1.Text = (hdd.Count == 0) ? "No match" : hdd[0].City;

Updated 27-Jan-14 18:11pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)