In MSSQL you can convert a string into an integer like this:
CONVERT(INT, table.column)
Is there any C# expression that Linq to SQL would translate to this?
In C# you can normally do the same by using int.Parse()
, but unfortunately, trying to use int.Parse()
in a Linq query results in an error:
Method 'Int32 Parse(System.String)' has no supported translation to SQL.
Is there any C# expression that Linq to SQL would translate to CONVERT(INT, ...)
?
C# 有Convert.ToInt32()
应该做你正在寻找的。
Convert.ToInt32 should work. Check out this article for information on the translations.
for LINQ to entities, if it is simply a mismatch between your model and the DB you can overcome this using a valueconverter with EF Core.
public OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<mymodel>()
.Property(e => e.myModelIntColumn) // this column is a string in the db
.HasConversion(new ValueConverter<int?, string>(m => m.HasValue ? m.ToString().Substring(0,10) : null, db => int.Parse(db ?? "0")));
}
and in my LINQ i can then use bit comparison:
MyDBSet.Where( e => (BitsWanted == 0 || ( e.myModelIntColumn & BitsWanted ) > 0));
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.