2017-06-12 64 views
1

首先,我看到類似這樣的問題,但由於這個原因,我在這裏問的問題還沒有解決我的問題。Linq查詢不支持System.String

我正在使用SQL Server,我想在C#中將LINQ轉換爲SQL。 這是我的表: enter image description here

這是我的代碼:

[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:「,字符串運算符類型」系統。字符串「不受支持。 enter image description here

+0

密碼作爲主鍵? –

+0

@RubensFarias是的,因爲我打算用MD5編碼存儲密碼,而且我不想重複密碼...... ID是一個自動增量字段,所以我不太確定是否必須將它標記爲主要關鍵也。 –

+2

我會將主鍵更改爲ID字段。只能有一個主鍵。另外,MD5哈希算法不是存儲密碼的最安全的方式。 – Reynevan

回答

2

你打電話Contains()超載需要char

這可能會混淆LINQ引擎。

'更改爲"以便您通過string並且它應該可以工作。

此外,請不要撥打.ToString();這可能也會搞砸了。

+0

Omg我覺得我很尷尬,至少我一直在尋找我的問題,thx!然後我看到我無法使用orderby username.Length ... –

0

包含總是找字符串值

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;