我不知道linq AT ALL。完成我们所有后端工作的人已经工作了2周,但我需要做的就是根据“术语”过滤 getItemList 。我已经尝试过搜索如何添加“where”子句,但我只是没有运气。
请注意,我在 getItemList 参数中添加了“字符串术语”。
请帮忙。
public IndexedItem getItem(string name) {
var repo = new Project.SQLServerDataManager.IndexItemRepository(ConfigurationManager.ConnectionStrings["ItemRepositoryConnstring"].ConnectionString);
return repo.getItem(name);
}
public object getItemList(string term) {
var repo = getRepo();
return from i in repo.getItem()
select new { name = i.name, itemType = i.itemType.name };
}
private IIndexedItemReadOnlyRepository getRepo() {
return new Project.SQLServerDataManager.IndexItemRepository(ConfigurationManager.ConnectionStrings["ItemRepositoryConnstring"].ConnectionString);
}
修改以澄清:“term”是搜索字词。它需要匹配项目“名称”
答案 0 :(得分:5)
这是你在找什么?
public object getItemList(string term) {
var repo = getRepo();
return from i in repo.getItem()
where i.Name == term
select new { name = i.name, itemType = i.itemType.name };
}
答案 1 :(得分:0)
尝试:
public object getItemList(string term) {
var repo = getRepo();
return from i in repo.getItem()
where i.Term = "Your Term"
select new { name = i.name, itemType = i.itemType.name };
}
答案 2 :(得分:0)
答案 3 :(得分:0)
您可以使用“where”两种方式进行过滤:
将其添加到您的代码中:
return from i in repo.getItem()
where i.Name == "Test Value"
select new { name = i.name, itemType = i.itemType.name };
或者:
return repo.getItem().Where(item => item.Name == "Test Value").Select(item => new { name = i.name, itemType = i.itemType.name });
我不知道我在哪里找到它,但是我已经为LINQ参考添加了书签,它解释了LINQ的基础知识以及如何使用这些函数:http://www.dotnetperls.com/linq