Linq query doesn't support System.String

时间:2017-06-12 16:44:24

标签: c# sql-server linq linq-to-sql

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: enter image description here

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. enter image description here

2 个答案:

答案 0 :(得分:2)

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.

答案 1 :(得分:0)

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;