Click to See Complete Forum and Search --> : C# 3.0 Lambdas


Suzi167
February 26th, 2009, 03:53 PM
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:



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

BigEd781
February 26th, 2009, 03:56 PM
You are using an assignment operator instead of an equality operator.


uow.Remove(uow.VqEmSampleSchedules.Select( s => s.VqEmTestByLocationId=id));


Should be


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.

Suzi167
February 26th, 2009, 08:46 PM
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

Suzi167
February 27th, 2009, 08:32 AM
I found an answer even though it's not a one-line solution it's a good one that works.



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