Linq中的Where子句和比较字符串

时间:2014-03-18 06:00:38

标签: c# linq

public static List<table1> CRBTsongformis(string sSname)
{
    List<table1> crbtlist = new List<table1>();

    using (crbt_onwebEntities dbContext = new crbt_onwebEntities())
    {
        crbtlist = (from z in dbContext.table1 
                    where z.CONTENT_NAME.Contains(sSname) && 
                    z.STATUS!=null select z).ToList();
    }

    return crbtlist;
}

在我的表1中,列名CONTENT_NAME包含&#39; Jiya&#39;和&#39; Jiya Re&#39;值。我只想要获取#Jiya&#39;当sSname参数包含&#39; Jiya&#39;只有&#39; Jiya Re&#39;当sSname包含&#39; Jiya Re&#39;

4 个答案:

答案 0 :(得分:2)

您需要使用==不包含:

where z.CONTENT_NAME == sSname

答案 1 :(得分:1)

尝试以下代码

public static List<table1> CRBTsongformis(string sSname)
    {
        List<table1> crbtlist = new List<table1>();
        using (crbt_onwebEntities dbContext = new crbt_onwebEntities())
        {
            crbtlist = (from z in dbContext.table1 where z.CONTENT_NAME == sSname 
            && z.STATUS!=null select z).ToList();
        }
        return crbtlist;
    }

答案 2 :(得分:1)

这更像是一个Linq问题,而不是MVC 3

public static List<table1> CRBTsongformis(string sSname)
{
    List<table1> crbtlist = new List<table1>();

    using (crbt_onwebEntities dbContext = new crbt_onwebEntities())
    {
        crbtlist = (from z in dbContext.table1 
                    where z.CONTENT_NAME==sSname && 
                    z.STATUS!=null select z).ToList();
    }

    return crbtlist;
}

会更准确

答案 3 :(得分:1)

按如下方式更改您的代码。

public static List<table1> CRBTsongformis(string sSname)
{
    List<table1> crbtlist = new List<table1>();

    using (crbt_onwebEntities dbContext = new crbt_onwebEntities())
    {
        crbtlist = (from z in dbContext.table1
                    where z.CONTENT_NAME.Contains(sSname) &&
                     !dbContext.table1.Any(e => e.CONTENT_NAME.Contains(sSname) && e.CONTENT_NAME.Length > sSname.Length) &&
                    z.STATUS != null
                    select z).ToList();
    }

    return crbtlist;
}