Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion 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 August 1st, 2011, 01:02 PM
Authorized User
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default Deleting oldest record by date using LINQ

Hi and TIA:

Using LINQ I would like to find the oldest dated record (just one) from a table. I have a date field when the record was inserted, (that also matches a certain customer value) and delete it.

What I have so far is:

var deleteOldest = from myTable in db.myTables where (myTable.Customer_ID == customer && myTable.Date_Played == MIN(Date_Played) select myTable;

foreach(var detail in deleteOldest)
{
db.myTables.DeleteOnSubmit(detail);
db.SubmnitChanges();
GridView1();
}

I am using the MIN substitution just as a "placeholder" as I know this is wrong but I'm trying to show the intent/logic of what I am trying to accomplish.

Kindest Regards,
Cliff
 
Old August 1st, 2011, 02:23 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Cliff,

You can do an OrderByDescending and then take the first item. Here's a quick example using LINQ to objects:

Code:
 
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      List<Person> people = new List<Person>() { new Person() { Id = 1, Name = "Imar" }, new Person() { Id = 2, Name = "Cliff" }, new Person() { Id = 3, Name = "Joe" } };
      Person oldest = people.OrderByDescending(p => p.Id).First();
    }
  }
  public class Person
  {
    public int Id { get; set; }
    public string Name { get; set; }
  }
}
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!
 
Old August 1st, 2011, 03:29 PM
Authorized User
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default LINQ and oldest record

I'm so sorry but relating this to a List and class Person just throws me off.

Perhaps I cannot use the var LINQ that is my only knowledge at this point?

I now have two non-working versions.

1)
Code:
var deleteOldest = from myTable in db.myTables where myTable.Customer_ID == customer orderby myTable.Date_Played descending select myTable.First();
2.
Code:
var deleteOldest = from myTable in db.myTables where myTable.Customer_ID == customer orderby myTable.Date_Played descending select myTable.OrderbyDescending.First();
Both give errors.
Sorry, but I am trying to related this to the var and foreach syntax

Regards,
Cliff
 
Old August 1st, 2011, 04:28 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Cliff

Quote:
I'm so sorry but relating this to a List and class Person just throws me off.
Sorry, somehow I thought you were using method syntax (e.g. Where(x => ... etc). I created a separate collection and class so I could provide you with a working example, rather than trying to write a query for your collections that I can't test.

Whether the source is a list or not doesn't really matter. What matters is that you're querying a LINQ compatible collection, so the idea is really the same.

Quote:
Sorry, but I am trying to related this to the var and foreach syntax
Since you're querying a single instance you don't really need var (although you could still use it). First returns an actual type; Person in my example, myTable in yours. The var short cut will still work though.
And since First() returns a single instance, you don't need foreach. So, when you get your example to work you can do this:

Code:
db.myTables.DeleteOnSubmit(detail);
db.SubmnitChanges();
GridView1();
without the foreach.

Here's a revised example showing the concept. The below code filters the people based on the CustomerId, sorts them by the PlayDate (no need for the descending as I said earlier) and then queries the first element which is effectively the oldest. You need to wrap the entire query in a pair of parentheses and then call First() on that query to get a single instance of Person.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      List<Person> people = new List<Person>() 
      { 
        new Person() { CustomerId = 1, Name = "Imar", PlayDate = new DateTime(2010, 1, 1) }, 
        new Person() { CustomerId = 1, Name = "Cliff", PlayDate = new DateTime(2011, 1, 1) }, 
        new Person() { CustomerId = 2, Name = "Joe", PlayDate = new DateTime(2010, 1, 1)  },
        new Person() { CustomerId = 2, Name = "Jane", PlayDate = new DateTime(2011, 1, 1) } 
      };
      var oldest = (from person in people
                    where person.CustomerId == 1
                    orderby person.PlayDate 
                    select person).First();
 
      // No need for a foreach. oldest *is* a person
      DateTime test = oldest.PlayDate;
 
      // The query above is functionally equivalent to:
      Person oldest = (from person in people .... rest of the code here
    }
  }
 
  public class Person
  {
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public DateTime PlayDate { get; set; }
  }
}
Hope this helps. If not, please let me know....

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:
esherr01 (August 1st, 2011)
 
Old August 1st, 2011, 05:19 PM
Authorized User
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default Conclusion

...well, no errors! Yea! I still have to add in more records for the testing but, looks great, thanks.

The only thing I added at at the end was:

people.DeleteOnSubmit(oldest);
people.SubmitChanges();
 
Old August 2nd, 2011, 03:44 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, correct. oldest now contains a single item so you can directly delete it from the context without looping over it.

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
Deleting An Earthquake Record braisj BOOK: Professional Android 2 Application Development 0 March 9th, 2011 11:08 PM
LINQ Query returning only 1 record digink BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 5 May 24th, 2010 12:50 PM
Problems In Deleting Record nkrust ASP.NET 1.0 and 1.1 Professional 2 January 12th, 2007 11:52 PM
Deleting All Record In A Table McDiddy Access VBA 3 January 10th, 2007 09:20 AM
Deleting A Record sirmilt BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 1 July 31st, 2006 07:12 PM





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