救命 !!如何更新具有int数据类型的记录 [英] help !! how to update record having int data type

查看:62
本文介绍了救命 !!如何更新具有int数据类型的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   void  upd()
{
amritEntities context = new amritEntities();
IEnumerable< timesheet> list = 来自 v in context.Timesheets
其中 v.projectid = 5
select v;
foreach var v in list)
{
v.projectid = Convert.ToInt32( 5 + 2 );
}
context.SaveChanges();
}
}
}





构建时出错

< pre lang =text>错误1无法将lambda表达式转换为类型'string',因为它不是委托类型C:\ Users \ user \ docs \ visual studio 2010 \Projects\ConsoleApplication49 \ConsoleApplication49 \ Class3.cs 15 43 ConsoleApplication49
错误3无法隐式转换类型'int?' to'bool'C:\ Users\user \documents\visual studio 2010 \Projects\ConsoleApplication49\ConsoleApplication49\Class3.cs 15 49 ConsoleApplication49
错误2委托'System.Func< ConsoleApplication49 .Timesheet,INT,布尔>不带1个参数C:\ Users \ user \documents \ visual studio 2010 \Projects\ConsoleApplication49 \ConsoleApplication49 \Class3.cs 15 43 ConsoleApplication49

解决方案

1.您必须使用与您的属性相同类型的常量值 Timesheets 实体类应该是;



2.因此,您必须查看 Timesheets 类的生成源代码,了解该属性 projectid ,如果其类型为 string ,则应在代码中使用字符串值,如下例所示:

 IEnumerable< timesheet> list = 来自 v  in  context.Timesheets 
其中 v.projectid = 5
选择 v;
< / 时间表 >





3.基于 projectid 是一个int的信息您应该像这样更改您的代码:

  public   void  upd()
{
amritEntities amritEntities context = new amritEntities()
//
// 建立您的查询
IQueryable< timesheet> list = 来自 v in context.Timesheets
其中 v.projectid = 5
select v;
//
// 运行查询==>结果
列表< timesheet> resultList = list.ToList();
//
// 修改ID。
foreach (时间表项 in resultList)
{
item.projectid = 7 ;
}
//
// 保存更改
context.SaveChanges();
}
}


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

解决方案

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:

            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:

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();
        }
}


这篇关于救命 !!如何更新具有int数据类型的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆