我正在通过 Visual Studio 使用 MySql LinQ 来处理实体。除了注释掉的部分之外,该代码可以正常工作。这适用于 SQL,但不适用于 MySQL。有谁知道被注释掉的代码的 MySql 等效项吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace TestMySql_Nuget_etc
{
class Program
{
static void Main(string[] args)
{
worldEntities db = new worldEntities();
var countries = from c in db.countries
where c.Name.StartsWith("A")
select c;
//string query = ((ObjectQuery)countries.ToTraceString());
foreach(var c in countries)
{
Console.WriteLine(c.Name);
}
Console.ReadLine();
}
}
}
最佳答案
您需要更详细地解释为什么它不起作用以及抛出什么样的异常/错误(如果是)。
首先,您无法将字符串直接拆箱
为另一种类型
(如ObjectQuery)。
您只需从基类拆箱
到继承类,反之亦然。
您可以执行以下操作:
int number = 30;
object obj = number;
number = (int) obj;// if it fails, will throw a cast exception
...
class A
{
public string Name {get;set;}
}
class B : A
{
public int Age {get;set;}
}
class C
{
public int Age {get;set;}
}
...
A objectA = new A();
A objectAAndB = new B();
B objectJustB = (B) objectAAndB;
B objectB = (B) A; // It'll throw a cast exception
并遵循一些你不能做的事情:
A objectA = new A();
B objectB = new B();
C objectC = new C();
A objectC1 = new C(); // It won't compile
C objectC2 = (C) objectA; // It won't compile
C objectC3 = (C) objectB; // It won't compile
A objectA1 = (A) objectC; // It won't compile
B objectB1 = (B) objectC; // It won't compile
希望对你有帮助。
关于mysql - Visual Studio 2015 MySql LinQ to Entities 获取生成的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36271067/