我正在编写一些查询 Visual Studio 对象模型的代码。
我看到集合对象上没有Exists
方法,Projects
但我确实喜欢防御性编程,而不是依赖于 try catch 块。所以我看到物体AsQueryable()
上有,我想知道这是否有帮助。Projects
我可以在这里看到我想写的那种代码,
IQueryable<Student> query =
objStudent.AsQueryable().Where(student => student.Name.Contains("Alavala"));
但对我来说,这就像
IQueryable<EnvDTE.Project> query =
sol.Projects.AsQueryable().Where(proj => proj.Name=project);
但这不会编译给出错误消息
“IQueryable”不包含“Where”的定义,并且找不到接受“IQueryable”类型的第一个参数的扩展方法“Where”(您是否缺少 using 指令或程序集引用?)
它只是缺少参考吗?这是最小的可重新创建代码...
using System.Linq;
using System.Runtime.InteropServices;
namespace AsQueryableConsoleApp
{
class Program
{
static void Main(string[] args)
{
/* ensure the solution is running in a separate instance of Visual Studio 2017 */
string solution = @"C:\Users\Simon\source\repos\WebApplication1\WebApplication1.sln";
string project = "WebApplication1";
//string projectItem = "WebForm1.aspx";
object objSol = Marshal.BindToMoniker(solution); /* should bind if running */
EnvDTE.Solution sol = (EnvDTE.Solution)objSol; /* does a cast */
/* next line does not compile with error message
Error CS1061
'IQueryable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'IQueryable' could be found (are you missing a using directive or an assembly reference?) AsQueryableConsoleApp
*/
IQueryable<EnvDTE.Project> query = sol.Projects.AsQueryable().Where(proj => proj.Name = project);
}
}
}