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
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)
{
}
}
ASKER
cannot convert type string to int
int[] myArray=new int[]{catId, 12 };
int[] myArray=new int[]{catId, 12 };
ASKER
catid has value like (1,2,3,4)
Sorry, I missed the point that catId is a string
string[] myArray=new string[]{catId, 12 };
ASKER
Error at
myArray.Contains(c.categor yid)
========================== =======
Error 15 'string[]' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.Queryable.Con tains<TSou rce>(Syste m.Linq.IQu eryable<TS ource>, TSource)' has some invalid arguments
myArray.Contains(c.categor
==========================
Error 15 'string[]' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.Queryable.Con
ASKER
string[] myArray=new string[]{catId, 12 };
error: at {catId, 12 };
Error 13 Cannot implicitly convert type 'int' to 'string'
error: at {catId, 12 };
Error 13 Cannot implicitly convert type 'int' to 'string'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.Con tains<TSou rce>(Syste m.Linq.IQu eryable<TS ource>, TSource)' has some invalid arguments
Error 14 'int[]' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.Queryable.Con
Did you add the using to your existing usings on the top of your code file ?
ASKER
yes its there
Can you try replacing cat.Contains(c.categoryid) by Enumerable.Contains(cat, c.categoryid) ?
ASKER
Error 13 The type arguments for method 'System.Linq.Enumerable.Co ntains<TSo urce>(Syst em.Collect ions.Gener ic.IEnumer able<TSour ce>, TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
still the error
still the error
var cat = ( from c in catid.Split( ',' ) select int.Parse( c ) ).ToList();
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 :
Open in new window