This question already has an answer here:
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
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
Here the line:
editordetail1(db,Convert.ToInt32(Server.HtmlEncode(Request.Cookies["j_id"].Value)));
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;
Try converting your return value from function: editordetail1
to a List as:
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;
}
you need to change the following lines:
var rr = editordetail1(db,Convert.ToInt32(Server.HtmlEncode(Request.Cookies["j_id"].Value)));
if (rr.Count() != 0)
to:
var rr = editordetail1.Invoke(db,Convert.ToInt32(Server.HtmlEncode(Request.Cookies["j_id"].Value))).FirstOrDefault();
if (rr != null)