我有一个字符串,其名称由逗号分隔,我需要为我所拥有的字符串执行Select Operation Using In子句..
这是我的代码
protected void btnSubmitt_Click(object sender, EventArgs e)
{
try
{
List<string> ids = new List<string>();
for (int i = 0; i < RadListBox2.Items.Count; i++)
{
ids.Add(RadListBox2.Items[i].Text);
}
string result = string.Join(",", ids);
//result I have Data Something like This
result=name1,name2
var ProductAttributeRowId = string.Join(",", from m in dt.AsEnumerable() where m.Field<string>("ProductAttributeName") in result select m.Field<long>("ProductAttributeRowId"));
string json = "{'ProductRowId':" + hdnId.Value+ ",'ProductAttributeRowId':'" + ProductAttributeRowId +"'}";
statuslabel.Text = ClsUtility.HttpPost(url + "Services/Product.svc/ProductAttributesID", json);
}
catch (Exception Err)
{
}
BindGrid();
}
当我使用'in'Linq查询请帮助我
答案 0 :(得分:0)
如果需要将ID分为两行,为什么要将ID与逗号连接起来?
将Contains
与原始列表一起使用,我想这就是您想要的:
var ProductAttributeRowId = dt.AsEnumerable()
.Where(row => ids.Contains(row.Field<string>("ProductAttributeName")))
.Select(row => row .Field<long>("ProductAttributeRowId"));
答案 1 :(得分:0)
要执行IN查询,请使用以下语法:
var array = new List<String>(...);
var contains = (from elm in collection where array.Contains(elm)).Any();
也就是说,你反转了逻辑:包含值的集合包含你的迭代器变量。
答案 2 :(得分:0)
var ProductAttributeRowId =string.Join("," ,dt.AsEnumerable()
.Where(row => ids.Contains(row.Field<string>("ProductAttributeName")))
.Select(row => row.Field<long>("ProductAttributeRowId")));