铸造INT字符串中的LINQ where子句 [英] Casting int to string in Linq where clause

查看:100
本文介绍了铸造INT字符串中的LINQ where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在LINQ查询,我要投一个int为字符串',其中'条款,我曾尝试阅读类似的交锋中SO,但我仍然有应用解决方案,我的方法的困难。这是会抛出System.Web.HttpUnhandledException错误的行,任何帮助是AP preciated:

|| (p.ProductID = NULL和放大器;&安培; p.ProductID.ToString()包含(S)!):

 公共无效搜索(字符串s)
    {
        CommerceEntities DB =新CommerceEntities();

            变种产品=从p在db.Products

                           其中(p.ModelName =空&安培;!&安培; p.ModelName.Contains(多个))
                           || (p.ProductID = NULL和放大器;!&安培; p.ProductID.ToString()包含(S))
                           || (p.ModelNumber = NULL和放大器;!&安培; p.ModelNumber.Contains(S))
                           || (p.Description = NULL和放大器;!&安培; p.Description.Contains(S))

                           选择新
                           {
                               //显示项目
                               的ProductID = p.ProductID,
                               ProductImage = p.ProductImage,
                               UnitCost = p.UnitCost
                           };

            ListView_Products.DataSourceID = NULL;
            ListView_Products.DataSource =产品;

    }
 

解决方案

在EF,有那么你需要转换为双或十进制没有过载对于int。你必须使用 SqlFunctions.StringConvert

这样的:

  p.ProductID = NULL和放大器;!&安培; SqlFunctions.StringConvert((双)p.ProductID)。载(S)
 

I have a 'where' clause in a linq query where I have to cast an int to a string, I have tried reading similar encounters in SO but I still have difficulties applying the solutions in my method. This is the line that will throw the 'System.Web.HttpUnhandledException' error, any help is appreciated:

|| (p.ProductID != null && p.ProductID.ToString().Contains(s)) :

  public void search(string s)
    {
        CommerceEntities db = new CommerceEntities();

            var products = from p in db.Products

                           where (p.ModelName != null && p.ModelName.Contains(s))
                           || (p.ProductID != null && p.ProductID.ToString().Contains(s))
                           || (p.ModelNumber != null && p.ModelNumber.Contains(s))
                           || (p.Description != null && p.Description.Contains(s))

                           select new
                           {
                               // Display the items 
                               ProductID = p.ProductID,
                               ProductImage = p.ProductImage,
                               UnitCost = p.UnitCost
                           };

            ListView_Products.DataSourceID = null;
            ListView_Products.DataSource = products;

    }

解决方案

In EF, There is no overload for int so you need to cast to a double or a decimal. You have to use SqlFunctions.StringConvert

Like this:

        p.ProductID != null &&  SqlFunctions.StringConvert((double)p.ProductID).Contains(s)

这篇关于铸造INT字符串中的LINQ where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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