List

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am creating a list as follows:
list<person>users = (from p in database.Users);

I get an error on list saying: "a get or set accessor expected"

What am I doing wrong?

Thanks,
Miguel
 
Well, I'd actually expect "A query body must end with a select clause
or a group clause"; any chance you could post something more
illustrative? this and the list<person> (rather than List<person>)
make it obvious that this isn't the real code, so we can't be sure we
are solving the real problem...

However! That message usually means that you have confused the syntax
of a method and property - i.e. (ignore the null, that isn't
important)

public %something% Foo { return null; }

should be either

public %something% Foo() { return null; } // method
or
public %something% Foo { get {return null; } } // property

Marc
 
shapper said:
I am creating a list as follows:
list<person>users = (from p in database.Users);

I get an error on list saying: "a get or set accessor expected"

What am I doing wrong?

Well for a start,
list<person>users
should be

List<Person> users

Spaces and capitalisation matter.

Secondly, query expressions in C# have to end with a select clause or a
group clause. So you *could* potentially have written:

List<Person> users = from p in database.Users select p;

but then it's far more straightforward to just write:

List<Person> users = database.Users;


However, that's still unlikely to work, because it's unlikely that the
Users property of database returns a list. You probably want:

List<Person> users = database.Users.ToList();
 
Well, I'd actually expect "A query body must end with a select clause
or a group clause"; any chance you could post something more
illustrative? this and the list<person> (rather than List<person>)
make it obvious that this isn't the real code, so we can't be sure we
are solving the real problem...

However! That message usually means that you have confused the syntax
of a method and property - i.e. (ignore the null, that isn't
important)

public %something% Foo { return null; }

should be either

public %something% Foo() { return null; } // method
or
public %something% Foo { get {return null; } } // property

Marc

Hi,

Yes, that was just a code example. I am trying to identify the
problem ...

The real code I have is this:

public list<TagPaper> GetTagPapers {

list<TagPaper> papers = (from t in database.Tags
select new TagPaper {
Tag = t,
Tie = new Tie {
Count = new Count {
File = t.FileTag.Count
}
}
}).ToList;
return papers;
}

Thanks,
Miguel
 
shapper said:
Yes, that was just a code example. I am trying to identify the
problem ...

The real code I have is this:

public list<TagPaper> GetTagPapers {

list<TagPaper> papers = (from t in database.Tags
select new TagPaper {
Tag = t,
Tie = new Tie {
Count = new Count {
File = t.FileTag.Count
}
}
}).ToList;
return papers;
}

It would have been really handy if you'd posted that to start with.

There are three things wrong here:

1) You're using "list" instead of "List", in both the method
declaration and the variable declaration.

2) You haven't put any parentheses at the end of the method signature.

3) You haven't put any parentheses at the end of the call to ToList().

It should be:

public List<TagPaper> GetTagPapers() {

List<TagPaper> papers = (from t in database.Tags
select new TagPaper {
Tag = t,
Tie = new Tie {
Count = new Count {
File = t.FileTag.Count
}
}
}).ToList();
return papers;
}
 
Well for a start,
list<person>users
should be

List<Person> users

Spaces and capitalisation matter.

Secondly, query expressions in C# have to end with a select clause or a
group clause. So you *could* potentially have written:

List<Person> users = from p in database.Users select p;

but then it's far more straightforward to just write:

List<Person> users = database.Users;

However, that's still unlikely to work, because it's unlikely that the
Users property of database returns a list. You probably want:

List<Person> users = database.Users.ToList();

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon_skeet
C# in Depth:http://csharpindepth.com

Jon,

but in my real code that is what I have (I Think)

public List<TagPaper> GetTagPapers {

List<TagPaper> papers = (from t in database.Tags
orderby t.FileTag.Count descending
select new TagPaper {
Tag = t,
Tie = new Tie {
Count = new Count {
File = t.FileTag.Count
}
}
}).ToList();
return papers;
}

Thanks,
Miguel
 
shapper said:
but in my real code that is what I have (I Think)

public List<TagPaper> GetTagPapers {

List<TagPaper> papers = (from t in database.Tags
orderby t.FileTag.Count descending
select new TagPaper {
Tag = t,
Tie = new Tie {
Count = new Count {
File = t.FileTag.Count
}
}
}).ToList();
return papers;
}

That's not what your "real code" looked like a minute ago - you were
using list<TagPaper> and you didn't have () at the end of the call to
ToList().

As far as I can see all that's missing from the above is () at the end
of GetTagPapers on the first line, but I'm afraid I don't have very
much faith that it's your genuine code any more...
 
That's not what your "real code" looked like a minute ago - you were
using list<TagPaper> and you didn't have () at the end of the call to
ToList().

As far as I can see all that's missing from the above is () at the end
of GetTagPapers on the first line, but I'm afraid I don't have very
much faith that it's your genuine code any more...

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon_skeet
C# in Depth:http://csharpindepth.com

Jon, I updated my real code to the suggestions you gave me! That is
why I posted the new version ...

But I still get the error ...
 
shapper said:
Jon, I updated my real code to the suggestions you gave me! That is
why I posted the new version ...

But I still get the error ...

Have you now fixed the first line to
public List<TagPaper> GetTagPapers() {
(note the parentheses)?

What error message are you getting now?
 
The real code I have is this:

public list<TagPaper> GetTagPapers {

list<TagPaper> papers = (from t in database.Tags
select new TagPaper {
Tag = t,
Tie = new Tie {
Count = new Count {
File = t.FileTag.Count
}
}
}).ToList;
return papers;
}

Still list and not List ...

Arne
 
Yes, that was just a code example.

OK; try taking the disk out, wipe both sides with a soft, lint-free
cloth, and retry.

That was just an answer example. What I mean is that it will help
everyone (yourself, other readers, and any potential answerers) if you
post somthing (to quote Jon) "Short but complete".
http://www.pobox.com/~skeet/csharp/complete.html

The point being: your original post didn't contain the actual problem
being reported ;-p

Marc
 
 > Yes, that was just a code example.

OK; try taking the disk out, wipe both sides with a soft, lint-free
cloth, and retry.

That was just an answer example. What I mean is that it will help
everyone (yourself, other readers, and any potential answerers) if you
post somthing (to quote Jon) "Short but complete".http://www.pobox.com/~skeet/csharp/complete.html

The point being: your original post didn't contain the actual problem
being reported ;-p

Marc

Hi,

I will try to create better posts ... and explain everything better. I
hope I did just that in my newest problem. :-)

And thank you to everybody that helped me out!

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Enumerable.Range 3
Order List Of Anonymous Type 1
Create List in Linq 4
String Array to List 4
List. CSV. Stream 2
Convert IQueryable to iCollection 0
Linq. String 5
Collection Intersection Helper 7

Back
Top