假设我有一个IEnumerable
IEnumerable<Students> = from a in context.Students select a
有没有办法执行
from a in context.Students select a
从字符串?
答案 0 :(得分:0)
你可以这样做:
Northwnd db = new Northwnd(@"c:\northwnd.mdf");
IEnumerable<Customer> results = db.ExecuteQuery<Customer>
(@"SELECT c1.custid as CustomerID, c2.custName as ContactName
FROM customer1 as c1, customer2 as c2
WHERE c1.custid = c2.custid"
);
然而,它首先也失去了使用ORM的一些主要好处。自动数据绑定和自动参数绑定(从而防止SQLi攻击)
答案 1 :(得分:0)
'有什么办法......?'
当然有。这基本上是您要运行的一段C#代码。所以你需要的是一种将这段代码编译成有效程序集,加载程序集并运行这段代码的方法。 .NET编译器平台(“Roslyn”)或甚至CodeDom可以帮助您。但请记住,这可能非常复杂。