How to Add record manually to an existing Linq query

Hi,

I would like to know how to add records manually to an existing Linq query

The testComp below returns 4 records after the 1st statement is executed, it then executes the next statement, but when I tried testComp.List().Count, it still returns 4 records. So, nothing is added. Is there anything missing from the code?

var testComp = (from p in dc.EmployeeVarComps
                             where (p.EmployeeID == curEmpID) 
                             select new { p.EmployeeID });

testComp.ToList().Add(new { EmployeeID = empID });

Also, in debug mode, I run the following in the debug window

testComp.ToList().Add(new { EmployeeID = empID });

And I got this message: Expression cannot contain anonymous types

This error doesn't pop up but only when I run in the debug window.

Please advice. Thanks in advance.


               
  • Edited by wkpli Thursday, June 18, 2015 3:22 PM
June 18th, 2015 2:18pm

Hi,

Per my understanding, you might want to add a new item along with executing a query.

After executing the code below:

var testComp = (from p in dc.EmployeeVarComps
                              where (p.EmployeeID == curEmpID) 
                              select new { p.EmployeeID });


The query is ended with a set of EmployeeID saved in the testComp object.

Then the code below:

testComp.ToList().Add(new { EmployeeID = empID });

would not make sense cause there is no submitting action.

To add a new item to a list, you can use the similar code as below:

CalendarEvent manager = new CalendarEvent();
manager.Title = "New event";
context.Calendar.InsertOnSubmit(manager);
context.SubmitChanges();

Thanks 

Patrick Liang

Free Windows Admin Tool Kit Click here and download it now
June 21st, 2015 11:10pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics