简体   繁体   中英

Linq query doesn't support System.String

First of all, I have seen questions similar to this, but I've not resolve my problem for this reason I am asking here.

I am working with SQL Server and I'd like to transform from LINQ to SQL, in C#. This is my table: 在此处输入图片说明

This is my code:

  [Table]  public class h
    {

       [Column] public string username;
       [Column(IsPrimaryKey =true)] public string password;
       [Column] public int id; 
    }

    class Program
    {
        static void Main(string[] args)
        {
            bsdDataContext cc = new bsdDataContext("Data Source=localhost;Initial Catalog=toolsdb;Persist Security Info=True;User ID=sa;Password=0359");

            Table<h> h_table = cc.GetTable<h>();

            IQueryable<string> query = from item in h_table where item.username.Contains('o') orderby item.username.Length select item.username.ToString();

            foreach (string item in query)
            {
                Console.WriteLine(item);
            }



            Console.ReadKey();
        }

And when I build my program I get the error, System.NotSupportedException: ' , String operators for type 'System.String' are not supported. 在此处输入图片说明

You're calling the Contains() overload that takes a char .

That's probably confusing the LINQ engine.

Change ' to " so that you're passing a string and it should work.

Also, don't call .ToString() ; that's probably also messing it up.

contains always finding string value

    IQueryable<string> query = from item in h_table where item.username.Contains('o') orderby item.username.Length select item.username.ToString();

replace above with

    IQueryable<string> query = from item in h_table where item.username.Contains("o") orderby item.username.Length select item.username;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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