加快用的LINQ查询编译到SQL [英] Speed up Linq to Sql with compiled queries

查看:145
本文介绍了加快用的LINQ查询编译到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经申请LINQ在我的项目,以SQL和它采取了很多时间,所以我做了一个搜索,使其迅速和我已经搜查,从花的这里

I have applied linq to sql in my project and it's taking a lot of time so I made a search to make it speedy and i have searched and took a reference from here

和我的.cs code是

And my .cs code is

public static Func<DataClassesDataContext, int, IQueryable<editor_j_inf>>
editordetail1 = CompiledQuery.Compile((DataClassesDataContext db, int a) =>
                 from p1 in db.editor_j_infs
                 where p1.ed_journal_id == a
                 orderby p1.editor_id descending
                 select p1);   //Its my precompile process

public void editordetail()
{
    DataClassesDataContext db = new DataClassesDataContext();
    var rr = editordetail1(db,Convert.ToInt32(Server.HtmlEncode(Request.Cookies["j_id"].Value)));
    if (rr.Count() != 0)
    {
        txt_jtitle.Text = rr.First().j_title;
        txtissn_p.Text = rr.First().issn_p;           
    }        
}

但这个错误就要为

but the error is coming as

推荐答案

下面的行:

<$c$c>editordetail1(db,Convert.ToInt32(Server.HtmlEn$c$c(Request.Cookies[\"j_id\"].Value)));

有可能返回一个 的IEnumerable&LT; T&GT; 。而当你调用计数() rr.Count()那么你已经列举你的结果。

is possible returning an IEnumerable<T>. And when you call Count() in rr.Count() then you have enumerated your results.

这就是为什么当你调用第一()下面,你试图枚举第二次。

That's why when you are calling First() below, you are trying to enumerate second time.

txt_jtitle.Text = rr.First().j_title;

尝试从功能转换的返回值: editordetail1 来一个列表如下:

var rr = editordetail1(db,Convert.ToInt32(Server.HtmlEncode(Request.Cookies["j_id"].Value)));
var rrList = rr.ToList();

和现在使用的计数的列表属性,然后调用优先()

And now use the Count property on List and then call First()

if (rrList.Count() != 0) // use the List rrList
    {
        txt_jtitle.Text = rr.First().j_title; 
        txtissn_p.Text = rr.First().issn_p;           
    } 

这篇关于加快用的LINQ查询编译到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆