如何在EntityFramework 4中将字符串转换为int [英] How to cast a string to int in EntityFramework 4

查看:166
本文介绍了如何在EntityFramework 4中将字符串转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


大家好,


由于新要求,我们必须将相同的翻译添加到当前开发中,内置实体 - 框架4,它使用SQL Server。因此,如果我们有警报,例如,我们需要通过检查翻译的某些值来提供数据,例如
标题或说明。 



表:


警报

- AlertId int 

- AlertTypeId ** int **

- AlertTextEnum ** int **



翻译

- FieldId int

- FieldName string

- 代码**字符串**

- 描述字符串

- LanguageId int





因此,如果我们想要获取AlertType的文本,我们必须连接两个表并与Translation.FieldName ==" AlertTypeEnum"以及Translation.Code == Alert建立关系。 AlertTypeEnum。主要是这样的。



$
这里我的脏方法在LinqPad上检查但是不适用于这个版本的Entity-Framework,因为我不能使用ToString方法。



                                           

  VAR O = Translations.Where(M => m.LanguageId == 1&安培;&安培;新列表与LT;字符串> {

       &NBSP ;      " AlertTypeId"

            } .Contains(m.FieldName));
$


      var list =来自提醒中的项目

                itemTranslation in  o item.AlertTypeId.ToString()等于itemTranslation.Code

到ps中



               选择新的 

                {

                                  AlertId = item.AlertId, 

                                  AlertTypeDescr = itemTranslation.Description

                } .ToList();



因为这不起作用,我试过在第一个查询之前带来并将代码解析为整数,但它也不起作用。 
$


    VAR O = Translations.Where(M => m.LanguageId == 1&安培;&安培;新列表与LT;字符串> {

       &NBSP ;    " AlertTypeId"

          }。载(m.FieldName)) 。

选择(x => new {

   Code =(int)(x.Code),

   FieldName = x.FieldName,

   Description = x.Description

})。ToList();
$


    var list =来自提醒中的项目

                             加入itemTranslation  o

     在    item.AlertTypeId等于itemTranslation.Code


                             选择新的 

                              {

                                            AlertId = item.AlertId, 

                                      AlertTypeDescr = itemTranslation.Description

       };
$


我得到"无法将类型'字符串'转换为'int'"。

¥ b $ b一些建议?我无法更改实体框架和模型的版本。


谢谢!



解决方案

我得到"无法将类型'字符串'转换为'int'"。


如果sting值是int表示,那么为什么不能对字符串数据使用Convert.ToInt()?


Hello everybody,

Because of new requirements, we must add same translations to the current development, built in Entity-Framework 4, and it uses SQL Server. So, if we have an alert, for example, we need to bring the data by checking on translation some values, such as its title or description. 

Table:

Alert
- AlertId int 
- AlertTypeId **int**
- AlertTextEnum **int**

Translation
- FieldId int
- FieldName string
- Code **string**
- Description string
- LanguageId int


So, if we want to get the text for AlertType, we have to join both tables and make a relation with Translation.FieldName == "AlertTypeEnum", and Translation.Code == Alert.AlertTypeEnum. Mainly, this is the way.


Here my dirty approach checked on LinqPad but does not work with this version of Entity-Framework, because I cannot use the ToString method.

                                           

  var o = Translations.Where(m => m.LanguageId == 1 && new List<string> {
                            "AlertTypeId"
                        }.Contains(m.FieldName));

     var list = from item in Alerts
                itemTranslation in  o item.AlertTypeId.ToString() equals itemTranslation.Code
into ps
from itemTranslation in ps.DefaultIfEmpty()
                select new 
                {
                                 AlertId = item.AlertId , 
                                 AlertTypeDescr = itemTranslation.Description
                }.ToList();

Because this doesn't work, I'd tried to bring before the first query and parse Code into an integer, but it doesn't work either. 

    var o = Translations.Where(m => m.LanguageId == 1 && new List<string> {
                        "AlertTypeId"
                    }.Contains(m.FieldName)).
Select (x => new {
   Code = (int)( x.Code),
   FieldName = x.FieldName,
   Description = x.Description
}).ToList();

    var list = from item in Alerts
                             join itemTranslation in  o
      on   item.AlertTypeId equals itemTranslation.Code
                             select new 
                             {
                                           AlertId = item.AlertId , 
                                     AlertTypeDescr = itemTranslation.Description
       };

I get "Cannot convert type 'string' to 'int'".

Some advice? I cannot change the version of entity framework and the models.

Thank you!

解决方案

I get "Cannot convert type 'string' to 'int'".

If the sting value is an int representation, then why can't you use a Convert.ToInt() on the string data?


这篇关于如何在EntityFramework 4中将字符串转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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