CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: C# 3.0 Lambdas

  1. #1
    Join Date
    Apr 2005
    Posts
    172

    C# 3.0 Lambdas

    Hello,

    I need to write a LINQ expression to remove all the entries from my VqEmSampleSchedules table which have their VqEmTestByLocationId == to the id I have passed to that function.
    I have all my mappings working and I have entities with the same names as the tables.
    I have the following expression which is not compiling:

    Code:
     EnvironmentalMonitoringModelUnitOfWork uow = new EnvironmentalMonitoringModelUnitOfWork();
     uow.Remove(uow.VqEmSampleSchedules.Select( s => s.VqEmTestByLocationId=id));
    I am new to LINQ and to lambdas so any help is very appreciated.

    Thanks

    Susan

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: C# 3.0 Lambdas

    You are using an assignment operator instead of an equality operator.

    Code:
     uow.Remove(uow.VqEmSampleSchedules.Select( s => s.VqEmTestByLocationId=id));
    Should be

    Code:
     uow.Remove(uow.VqEmSampleSchedules.Select( s => s.VqEmTestByLocationId == id));
    Notice the extra '=' sign? Also, it always helps to look at and post the compiler error.

  3. #3
    Join Date
    Apr 2005
    Posts
    172

    Re: C# 3.0 Lambdas

    Oh... thanks for the correction... but unfortunately there is something else wrong with the query I have.
    Let me make it a bit more general

    What is the LINQ expression to remove certain records from an entity. For example if I have a table/entity called user I need to write a LINQ expression to remove all users with last name Smith.
    How can I do that.

    Thanks

    Susan

  4. #4
    Join Date
    Apr 2005
    Posts
    172

    Re: C# 3.0 Lambdas

    I found an answer even though it's not a one-line solution it's a good one that works.

    Code:
     EnvironmentalMonitoringModelUnitOfWork uow = DataService.Context.CreateUnitOfWork();
     //select all the entries with the id of that template
     var scheduleEntries = from scheduleEntry in uow.VqEmSampleSchedules where scheduleEntry.VqEmTestByLocationId == id select scheduleEntry;
      foreach (VqEmSampleSchedule sample in scheduleEntries)
                uow.Remove(sample);
      uow.SaveChanges();
      return RedirectToAction("Calendar");
    Susan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured