dilipkumar1007
dilipkumar1007

Reputation: 363

Where clause in Linq and compare strings

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

In my table1, column name CONTENT_NAME contain 'Jiya' and 'Jiya Re' values. I want to fetch only 'Jiya' when sSname argument contain 'Jiya' and only 'Jiya Re' when sSname contain 'Jiya Re'

Upvotes: 2

Views: 6417

Answers (4)

user3383479
user3383479

Reputation:

It is more a Linq Question than 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;
}

It will be more accurate

Upvotes: 1

Alborz
Alborz

Reputation: 6903

Change your code as follows.

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

Upvotes: 1

highwingers
highwingers

Reputation: 1669

You need to use == not contain:

where z.CONTENT_NAME == sSname

Upvotes: 2

Kamlesh
Kamlesh

Reputation: 529

Try following code

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

Upvotes: 1

Related Questions