我正在尝试使用LINQ来比较用户在搜索栏中输入的内容与表格的对比。
public ActionResult GetLibrary(string searchString, string lib)
{
var libraries = Database.Session.Query<Library>().ToArray();
string searchedLib = (from l in libraries
where l.CR_Name.Contains(searchString)
select new Library()
{
CR_Name = l.CR_Name
}).ToString();
lib = searchedLib;
return RedirectToAction(actionName: "GetLibInfo",
controllerName: "Home",
routeValues: new { lib });
}
而不是返回实际值,而是返回此信息: “System.Linq.Enumerable + WhereSelectArrayIterator'2 [Models.ModelName,Models.ModelName]”
答案 0 :(得分:4)
WhereSelectArrayIterator
是表示一组结果的IEnumerable
。如果你只想要/期望一个&#34;行,&#34;使用Single()
或First()
。
var searchedLib = (from l in libraries
where l.CR_Name.Contains(searchString)
select l).First();
var name = searchedLib.CR_Name;
最简单的方法:
var name = libraries.First(l => l.CR_Name.Contains(searchString)).CR_Name