日期:2014-05-20  浏览次数:20863 次

linq to sql直接用sql查询语句的问题
在微软的文档中,对于linq to sql的sql查询有这个例子:
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"
);
 
这个虽然可以实现查询的功能,但是事先已经定义了输出结果为Customer类
我的问题是:
我在某个db中任意查询一句语句:比如:
select * from mytable;
select * from yourtable;
也就是说我事先不知道查询语句,请问这个情况如何实现?
把Customer类换成Object的话,我如何后面获取获得的数据进行下一步操作?

------解决方案--------------------
既然用了linq, 那还用什么SQL语句
既然要用SQL语句,那就直接用Ado
------解决方案--------------------
用了Linq就不要用SQL。

用SQL就用ADO。
------解决方案--------------------
可以试试PDF.NET,即可以支持SQL方式,也支持ORM方式,例如你的例子可以这样写:

Northwnd db = MyDB.GetDBHelperByConnectionName("northwnd");
Customer c1=new Customer();
c1.custid=100;
EntityQuery<Customer> query=new EntityQuery<Customer>(db);
OQL q=OQL.From(c1).Select(c1.custid,c1.custName).Where(c1.custid).End;
List<Customer> results=query.QueryList<Customer>(q);