如何将Linq中的int转换为实体中的字符串

Shw*_*eta 3 c# linq

我在字符串(varchar)中的Db列,我需要将它分配给一个int值.我正在使用linq进行查询.虽然代码编译在运行时遇到错误.提前致谢.

PFB我的查询:

var vlauesCap = from plan in entities.PA_RTM_CAP_Group
                select new Business.PartnerProfile.LookUp
                {
                 Id =Convert.ToInt32(plan.cap_group_code),
                 //(Int32)plan.cap_group_code,
                 Value = plan.cap_group_name
                      };
                   return vlauesCap.ToList();
Run Code Online (Sandbox Code Playgroud)

Jef*_*ata 7

EF提供程序不知道如何转换Convert.ToInt()为可以针对数据库运行的SQL.您可以将结果拉回来并使用linq对象进行转换,而不是在服务器上进行转换:

// the ToList() here causes the query to be executed on the server and
// the results are returned in a list of anonymous objects
var results = (from plan in entities.PA_RTM_CAP_Group 
               select new 
               { 
                   Code = plan.cap_group_code, 
                   Name = plan.cap_group_name 
               }).ToList();

// the conversion can now be done here using Linq to Objects
var vlauesCap = from r in results
                select new Business.PartnerProfile.LookUp  
                {  
                    Id = Convert.ToInt32(r.Code),
                    Value = r.Name
                };  

return vlauesCap.ToList();  
Run Code Online (Sandbox Code Playgroud)