Link to home
Start Free TrialLog in
Avatar of ExpertHelp79
ExpertHelp79

asked on

Implement SQL IN clause in LINQ

Hello
in the below query
where c.categoryid == catid
categoryid is a integer feild in the table, i need to implement IN , but i am unable to get the contains method. Please help me to implement IN clause in the below code

TIA
SOS
public void LoadSubCategory(CheckBoxList chkSubCat, string catid)
        {
            try
            {
                chkSubCat.DataSource = from c in GAEntity.subcategorymasters
                                    orderby c.subcatdisplayname
                                    where c.categoryid == catid && c.status == 1 && c.deleted == 0
                                       select new { c.subcatid, c.subcatdisplayname };


                chkSubCat.DataTextField = "subcatdisplayname";
                chkSubCat.DataValueField = "subcatid";
                chkSubCat.DataBind();
            }
            catch (Exception Ex)
            {

            }
        }

Open in new window

Avatar of neonp
neonp
Flag of France image

Hello,

Why do you need an IN whereas you have only a single value ? To have an IN clause, you need to have an array of int and call somethin like the following :
public void LoadSubCategory(CheckBoxList chkSubCat, string catid)
        {
            try
            {
int[] myArray=new int[]{catId, 12 };
                chkSubCat.DataSource = from c in GAEntity.subcategorymasters
                                    orderby c.subcatdisplayname
                                    where myArray.Contains(c.categoryid) && c.status == 1 && c.deleted == 0
                                       select new { c.subcatid, c.subcatdisplayname };


                chkSubCat.DataTextField = "subcatdisplayname";
                chkSubCat.DataValueField = "subcatid";
                chkSubCat.DataBind();
            }
            catch (Exception Ex)
            {

            }
        }

Open in new window

Avatar of ExpertHelp79
ExpertHelp79

ASKER

cannot convert type string to int

int[] myArray=new int[]{catId, 12 };
catid has value like (1,2,3,4)
Sorry, I missed the point that catId is a string

string[] myArray=new string[]{catId, 12 };

Open in new window

Error at
myArray.Contains(c.categoryid)
=================================

Error      15      'string[]' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.Queryable.Contains<TSource>(System.Linq.IQueryable<TSource>, TSource)' has some invalid arguments      
string[] myArray=new string[]{catId, 12 };
error: at {catId, 12 };
Error      13      Cannot implicitly convert type 'int' to 'string'      
ASKER CERTIFIED SOLUTION
Avatar of neonp
neonp
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cat.Contains(c.categoryid)  at this line

Error      14      'int[]' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.Queryable.Contains<TSource>(System.Linq.IQueryable<TSource>, TSource)' has some invalid arguments      
Did you add the using to your existing usings on the top of your code file ?
yes its there
Can you try replacing cat.Contains(c.categoryid) by Enumerable.Contains(cat, c.categoryid) ?
Error      13      The type arguments for method 'System.Linq.Enumerable.Contains<TSource>(System.Collections.Generic.IEnumerable<TSource>, TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly.      

still the error
var cat = ( from c in catid.Split( ',' ) select int.Parse( c ) ).ToList();