Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public void upd()
        {
            amritEntities context = new amritEntities();
            IEnumerable<timesheet> list = from v in context.Timesheets
                                          where v.projectid = 5
                                          select v;
            foreach (var v in list)
            {
                v.projectid = Convert.ToInt32(5 + 2);
            }
            context.SaveChanges();
        }
}
}


errors while build
Error   1   Cannot convert lambda expression to type 'string' because it is not a delegate type C:\Users\user\documents\visual studio 2010\Projects\ConsoleApplication49\ConsoleApplication49\Class3.cs 15  43  ConsoleApplication49
Error   3   Cannot implicitly convert type 'int?' to 'bool' C:\Users\user\documents\visual studio 2010\Projects\ConsoleApplication49\ConsoleApplication49\Class3.cs 15  49  ConsoleApplication49
Error   2   Delegate 'System.Func<ConsoleApplication49.Timesheet,int,bool>' does not take 1 arguments   C:\Users\user\documents\visual studio 2010\Projects\ConsoleApplication49\ConsoleApplication49\Class3.cs 15  43  ConsoleApplication49
Posted
Updated 26-Aug-14 19:04pm
v2
Comments
Prasad Avunoori 27-Aug-14 0:58am    
What do you mean by this v.projectid = Convert.ToInt32(5 + 2);? Why are you mentioning 5+2?
Member 10879484 27-Aug-14 1:04am    
add +2 on the record
Prasad Avunoori 27-Aug-14 1:06am    
What is the use of adding 2 to 5 every time?
Prasad Avunoori 27-Aug-14 1:10am    
I think you are supposed to convert from v in context.Timesheets
where v.projectid = 5
select v; to a list
like below.

(from v in context.Timesheets
where v.projectid = 5
select v).ToList();
PIEBALDconsult 27-Aug-14 1:06am    
What's the point of the Convert.ToInt32 ? 5 + 2 is already an Int32.

1 solution

1.You must use constants values from the same type as your properties from Timesheets entity class are expected to be;

2.So you have to look on the generated source code of your Timesheets class for the property projectid, and if its type is string you should use string values in your code like in the next example:
C#
            IEnumerable<timesheet> list = from v in context.Timesheets
                                          where v.projectid = "5"                                                    
                                          select v;
</timesheet>


3.Based on the info that your projectid is an int you should change your code like this:
C#
public void upd()
        {
            amritEntities amritEntities context = new amritEntities()
            //
            //Build your query
            IQueryable<timesheet> list = from v in context.Timesheets
                                          where v.projectid = 5
                                          select v;
            //
            // Run the query ==> the results
            List<timesheet> resultList = list.ToList();
            //
            //Modify the IDs.
            foreach (timesheet item in resultList )
            {
                item.projectid = 7;
             }
            //
            //Save the changes 
            context.SaveChanges();
        }
}
 
Share this answer
 
v2
Comments
Member 10879484 27-Aug-14 2:00am    
its int only
Raul Iloc 27-Aug-14 2:03am    
1.Did you check it like I suggested, because if its "int" your above LINQ should work without problem!?
2.After the LINQ expression try to execute it into a "List" by using: "list.ToList()", then inspect each element in Debug, before to execute the for!
Member 10879484 27-Aug-14 6:17am    
still the errors comimg. Any other solution on how to implicate update
crud methods ?
Raul Iloc 27-Aug-14 6:46am    
See my update (the 3rd point) in my solution above!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900