Linq. Why do I get this error?

S

shapper

Hello,

I have 3 tables:

Tags > TagID, Name
Files > FileID, Name, Url
FilesTags > FileID, TagID

FilesTags relates each file with one or more tags

I need to get a specific Tag, given its ID, and get also the number of
associations to files. I created the following:

TagPaper tagPaper = (from t in database.Tags
select new TagPaper {
Tag = t,
Count = t.FilesTags.Count
}).SingleOrDefault(t => t.TagID == id);

I am getting an error in "Count = t.FilesTags.Count" saying:

'Tag' does not contain a definition for 'FilesTags' and no extension
method 'FilesTags' accepting a first argument of type 'Tag' could be
found (are you missing a using directive or an assembly reference?).

Any idea what am I doing wrong?

Thanks,
Miguel
 
J

Jon Skeet [C# MVP]

I have 3 tables:

Tags > TagID, Name
Files > FileID, Name, Url
FilesTags > FileID, TagID

FilesTags relates each file with one or more tags

I need to get a specific Tag, given its ID, and get also the number of
associations to files. I created the following:

     TagPaper tagPaper = (from t in database.Tags
                           select new TagPaper {
                             Tag = t,
                             Count = t.FilesTags.Count
                           }).SingleOrDefault(t => t.TagID == id);

I am getting an error in "Count = t.FilesTags.Count" saying:

'Tag' does not contain a definition for 'FilesTags' and no extension
method 'FilesTags' accepting a first argument of type 'Tag' could be
found (are you missing a using directive or an assembly reference?).

Any idea what am I doing wrong?

Well it looks like you haven't got a relationship from Tag to
FilesTags. When you type t. what does Intellisense suggest?

Jon
 
S

shapper

Well it looks like you haven't got a relationship from Tag to
FilesTags. When you type t. what does Intellisense suggest?

Jon

I found the error ... When I created the DBML file VS created FileTags
instead of FilesTags ... Then I renamed it to FileTag but the name was
not updated in cs. I did it directly on the code now and it is
working.

Thanks,
Miguel
 
S

shapper

Well it looks like you haven't got a relationship from Tag to
FilesTags. When you type t. what does Intellisense suggest?

Jon

Oops found a new problem on:

SingleOrDefault(t => t.TagID == id)

Is says:
'TagPaper' does not contain a definition for 'TagID' and no extension
method 'TagID' accepting a first argument of type TagPaper' could be
found (are you missing a using directive or an assembly reference?)

But isn't t referring to Tag? Or after I create TagPaper it is updated
an refers to TagPaper?

I was able to solve it using:
TagPaper tagPaper = (from t in database.Tags
select new TagPaper {
Tag = t,
Count = t.FileTag.Count
}).SingleOrDefault(t => t.Tag.TagID == id);

But it is confusing.

Thanks
Miguel
 
J

Jon Skeet [C# MVP]

shapper said:
Oops found a new problem on:

SingleOrDefault(t => t.TagID == id)

Is says:
'TagPaper' does not contain a definition for 'TagID' and no extension
method 'TagID' accepting a first argument of type TagPaper' could be
found (are you missing a using directive or an assembly reference?)

But isn't t referring to Tag? Or after I create TagPaper it is updated
an refers to TagPaper?

No, you're not in the query expression any more. The argument to
SingleOrDefault is a completely separate lambda expression.

Why not put a where clause in the query expression instead, and then
just call SingleOrDefault with no arguments?

TagPaper tagPaper = (from t in database.Tags
where t.TagID == id
select new TagPaper {
Tag = t, Count = t.FileTag.Count
}).SingleOrDefault();
 

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

Top