我正在尝试使用Visual Studio C#中的列表项来使用Linq概念。我收到了错误:
非静态字段,对象,属性,方法
需要对象引用
用于书单。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Linq.List
{
public class Books
{
string Title="title";
string Author="author";
string Publisher="publisher";
int Year=2000;
public List <Books> booklist = new List<Books>
{
new Books
{
Title="Programming C#",
Author="Jesse Liberty",
Publisher="O'reilly",
Year=2008
},
new Books
{
Title="Learning C#",
Author="Jesse Liberty",
Publisher="O'reilly",
Year=2008
},
new Books
{
Title="Programming php",
Author="Kevin Ludorf",
Publisher="O'reilly",
Year=2008
}
};
static void Main()
{
var ResultAuthor1= from b in booklist where b ResultAuthor1=="JesseLIberty"
select new { b.Title,b.Author};
foreach(var r in ResultAuthor1)
{
Console.WriteLine(r.Title+" by "+r.Author);
}
}
}
} // please rectify this code if there is any error
答案 0 :(得分:2)
您可以从SébastienSevrin将上面列出的静态列表标记为静态,也可以在Main中创建books
- 类的实例:
Books books = new Books();
var ResultAuthor1= from b in books.booklist where b.Author == "JesseLIberty"
答案 1 :(得分:1)
您的书单应该是静态的:
public static List <Books> booklist = new List<Books>
{
new Books
{
Title="Programming C#",
Author="Jesse Liberty",
Publisher="O'reilly",
Year=2008
},
new Books
{
Title="Learning C#",
Author="Jesse Liberty",
Publisher="O'reilly",
Year=2008
},
new Books
{
Title="Programming php",
Author="Kevin Ludorf",
Publisher="O'reilly",
Year=2008
}
};
修改强>
看起来你正在寻找文件:
Here是此错误的MSDN页面
Here是另一个MSDN页面,其中包含有关静态类的更多详细信息。
答案 2 :(得分:1)
更改
public List <Books> booklist = new List<Books>
到
public static List <Books> booklist = new List<Books>