Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5 > BOOK: Beginning ASP.NET 4.5 : in C# and VB
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 25th, 2013, 08:34 PM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
Default LINQ to Entities And Handling Gridview Events

Hi,
I think I searched half of the internet in one month and still can't find the solution...
I'm using LINQ to Entities for searching and then assign it to the DataSource of the Gridview...
The problem is handling sorting and paging events...
I think some people know how to handle these events when using LINQ to SQL, But I need them for LINQ to Entities...
Is it that hard when using LINQ to Entities???
Please tell me if I have to bring more information,
Thanks :)
.....

Edited : I think I had to posted this in general discussion forum, but now I don't know how to move it there... Sorry Mr.Spaanjaars :(...

Last edited by Kourosh94; September 25th, 2013 at 08:49 PM..
 
Old September 28th, 2013, 09:46 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

One way to do this is to use the Dynamic LINQ library that lets you perform string based searches. You can install it using NuGet: https://www.nuget.org/packages/DynamicQuery/ More info can be found here: http://weblogs.asp.net/scottgu/archi...y-library.aspx

The various events then expose data about sorting and paging which you can use to build up your LINQ query.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Kourosh94 (September 28th, 2013)
 
Old September 28th, 2013, 11:47 AM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
Default

Hi Imar,
I used the dynamic LINQ when you told me...
But I Couldn't find the events implementation again...
......
Here is an artical I found it similar : http://forums.asp.net/post/1177923.aspx
So until here, I have used LINQ to SQL, LINQ to Entities and Dynamic LINQ for it, but none of them works...
Maybe I'm doing something wrong...
Anyway thank you for answering Imar... I really appreciate it... :)
 
Old September 28th, 2013, 11:55 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

>> But I Couldn't find the events implementation again.

Not sure what you mean with this, nor what the actual problem you're having is.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Kourosh94 (September 28th, 2013)
 
Old September 28th, 2013, 12:31 PM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
Default

I mean I used Dynamic LINQ, but still can't figure out how to implement the sorting and paging events...
 
Old September 28th, 2013, 02:03 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Can you post the code you have so far, along with a description of your EF model?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old September 28th, 2013, 03:04 PM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
Default

Code for Search Button I've used with LINQ :

protected void bSearch_Click(object sender, EventArgs e)
{
using (CostPriceEntities myEntities = new CostPriceEntities())
{
GridView1.DataSourceID = null;
var data = (from a in myEntities.Assembles
where a.CodeA.Contains(tbCode.Text)
&& a.MESCA.Contains(tbMESC.Text)
&& a.KindA.Contains(ddlKind.Text)
&& a.NameA.Contains(tbName.Text)
select a).ToList();
GridView1.DataSource = data;
GridView1.DataBind();
}
}

Code for Search Button I'm using with Dynamic LINQ

protected void Button1_Click(object sender, EventArgs e)
{
using (CostPriceEntities myEntities=new CostPriceEntities())
{
GridView1.DataSourceID = null;
var data = myEntities.Assembles
.Where("NameA.Contains(@0) AND CodeA.Contains(@1) AND MESCA.Contains(@2) AND KindA.Contains(@3)", tbName.Text,tbCode.Text,tbMESC.Text,ddlKind.Text). ToList();
GridView1.DataSource = data;
GridView1.DataBind();
}
}

My EF has 3 tables : Assemble, Part, Process... they have relationships
And the codes above are using for searching through Assemble table...
with both LINQ or Dynamic LINQ, Gridview need to handle paging and sorting events...
if I have to bring more information, Please tell me...
thanks again:)
 
Old September 28th, 2013, 03:53 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I think the next steps are as follows:

1. Move the code that loads your data to a separate method.
2. Call that new method from the current events such as Click
3. Handle the Sorting event as explained in the article you linked to
4. From that method, retrieve e.SortExpression and store it in a field or property
5. Call the data loading method from step 1
6. Inside the data loading method, make use of the sort expression by expanding the query with an OrderBy statement that you pass the sort expression.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Kourosh94 (September 28th, 2013)
 
Old September 28th, 2013, 04:59 PM
Authorized User
Points: 75, Level: 1
Points: 75, Level: 1 Points: 75, Level: 1 Points: 75, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
Default

Hi there,

I wanna thank you Imar for helping me...
I do everything you told me, and it worked... I just changed a few...
here is the code for sorting event :
using (CostPriceEntities myEntities = new CostPriceEntities())
{
var data = myEntities.Assembles
.Where("NameA.Contains(@0) AND CodeA.Contains(@1) AND MESCA.Contains(@2) AND KindA.Contains(@3)", tbName.Text, tbCode.Text, tbMESC.Text, ddlKind.Text)
.OrderBy(e.SortExpression).ToList();
GridView1.DataSource = data;
GridView1.DataBind();
}
the sortdirection always is Ascending, after some searched in internet, I found it out the gridview only change the sortdirection when it's datasource assign to a datasource control...
But anyway, Now my Nightmare(sorting gridview when using LINQ) is gone thanks to you Imar...
Hope this post helps someone in future...
 
Old September 29th, 2013, 06:23 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You can find a solution to support ascending and descending order with the GridView here: http://stackoverflow.com/questions/2...ways-ascending

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!




Similar Threads
Thread Thread Starter Forum Replies Last Post
A simple LINQ to Entities Example (Problem in this Try it out) CompactDisc20 BOOK: Beginning ASP.NET 4 : in C# and VB 5 July 6th, 2013 10:16 AM
Best way to concenate strings in LINQ to Entities? IceThatJaw BOOK: Beginning ASP.NET 4 : in C# and VB 1 December 26th, 2011 05:46 AM
Linq Question about related entities gspro BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 0 March 23rd, 2010 09:29 AM
Comparing Performance Linq To entities and outofBand SQL updates urpcor BOOK: Professional ADO.NET 3.5 with LINQ and the Entity Framework ISBN: 978-0-470-22988-0 4 September 28th, 2009 06:22 AM
Handling Events with FAXCOMEXLib hugh@kmcnetwork.com VB How-To 0 April 14th, 2005 04:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.