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.
答案 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;