C# Linq查询不支持System.String

C# Linq查询不支持System.String,c#,sql-server,linq,linq-to-sql,C#,Sql Server,Linq,Linq To Sql,首先,我见过类似的问题,但我没有解决我的问题,因为我在这里问这个问题 我正在使用SQL Server,我想用C从LINQ转换为SQL。 这是我的桌子: 这是我的代码: [Table] public class h { [Column] public string username; [Column(IsPrimaryKey =true)] public string password; [Column] public int id;

首先,我见过类似的问题,但我没有解决我的问题,因为我在这里问这个问题

我正在使用SQL Server,我想用C从LINQ转换为SQL。 这是我的桌子:

这是我的代码:

  [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();
        }
当我构建程序时,会出现错误System.NotSupportedException:',不支持类型为“System.String”的字符串运算符。

您正在调用接受字符的Contains重载

这可能会混淆LINQ引擎

将“更改为”,以便您正在传递字符串,并且它应该可以工作


另外,不要打电话给.ToString;这可能也把事情搞砸了。

包含始终查找字符串值

    IQueryable<string> query = from item in h_table where item.username.Contains('o') orderby item.username.Length select item.username.ToString();
将上述内容替换为

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

密码作为主键?@RubensFarias是的,因为我要用MD5编码来存储密码,我不想要重复的密码。。。ID是一个自动递增字段,所以我不太确定是否必须将其标记为主键。我会将主键更改为ID字段。只能有一个主键。另外,MD5散列算法并不是存储密码的最安全的方法。@Reynevan我会更好地使用sha256…@JoseAviles试一试:如果你想要最大的安全性,这可能值得一读。天哪,我觉得我很尴尬,我已经至少搜索了1小时关于我的问题,谢谢!然后我发现我不能使用orderby用户名。长度。。。