|
-
February 26th, 2009, 04:53 PM
#1
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
-
February 26th, 2009, 04:56 PM
#2
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.
-
February 26th, 2009, 09:46 PM
#3
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
-
February 27th, 2009, 09:32 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|