[EF]执行SQL语句

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://david.blog.csdn.net/article/details/91517536

//using System.Data.Entity.Infrastructure;

    using (var context = new KTStoreContext())
            {
                try
                {
                    String sql = "SELECT * FROM Product ";
                    DbSqlQuery<Product> query = context.Products.SqlQuery(sql);
                    List<Product> products = query.ToList();

                    foreach (Product product in products)
                    {
                        Console.WriteLine("{0}\t售价:{1}", product.Name, product.Price);
                    }
                    Console.WriteLine("\n商品项数:{0}", products.Count());
                }
                catch (Exception ex) {
                    Console.WriteLine("\n错误信息:{0}\n",ex.Message);
                    Console.WriteLine(ex.ToString());
                }
            }
            Console.ReadKey();
展开阅读全文

SQL语句改成LINQ TO ef

09-19

由于某个delphi程序要用C/S改成B/S,临时抱佛脚,用ASP.NET MVC EF5 C#来做rn以前程序都是直接用SQL语句进行查询,现在用EF,单表操作很简单,多表问题来了rnrn一、SQL语句,实际上连接了很多个表和子查询,不过我简化了一下,表达意思就行了rn[code=sql]rnSELECT COPMA.MA002 AS 客户简称,COPTC.TC001 AS 订单单别, COPTC.TC002 AS 订单单号, rnCOPTD.TD003 AS 订单序号,CONVERT(datetime,COPTD.TD013) 交货日,rnCONVERT(datetime,COPTC.TC003) AS 订单日期, rnCOPTD.TD016 订单状态,COPTD.TD004 AS 品号 FROM COPTD rnINNER JOIN COPTC ON COPTD.TD001 = COPTC.TC001 AND COPTD.TD002 = COPTC.TC002 rnleft JOIN COPMA ON COPTC.TC004 = COPMA.MA001 rnWHERE 1=1 and rnCOPTC.TC003 >='20140701' AND COPTC.TC003 <='20140716' rn[/code]rnrnrn二、我照抄的版主q107770540linq语句并稍做改过,很巧,几年前q107770540解答类似问题时也是使用的同样的数据库rn[code=csharp]rn var query = from cc in erp.COPTCrn join cd in erp.COPTDrn on new cc.TC001, cc.TC002 equals new cd.TD001, cd.TD002 rn into mrn from n in m.DefaultIfEmpty()rn join ma in erp.COPMArn on new cc.TC004 equals new ma.MA001rn where rn Convert.ToDateTime(cc.TC003) > Convert.ToDateTime("2014-07-01")rn && Convert.ToDateTime(cc.TC003) < Convert.ToDateTime("2014-07-31")rn select newrn rn cc,rn cdrn ;rn[/code]rnrn报错:rn[color=#FF0000]join 子句中其中一个表达式的类型不正确。对“GroupJoin”的调用中的类型推理失败。[/color]rnrn另外,我的乱想,由于刚入门,可能见笑了rn对IEnumerable反复查询,连接,可以达到相关效果吗?rnrn虽然,可以用以前的方法,ado.net 循环读取DataReader,生成类再传递 给视图,但总觉得不好rnrn级别不够,份很少,见谅 论坛

没有更多推荐了,返回首页