Hello,

I have 3 tables with the following fields:

Posts: PostID, Title, Body
Tags: TagID, Name
PostsTags: PostID, TagID

1. I need to select all Tags (TagID and Name) that exist in PostsTags
adding a field Count which holds the frequency of how often each tag
is associated to a post, i.e., the number of times it shows in
PostsTags.

2. And would be also possible to group the tags as follows:
If Count <= 10 then group all tags and give Weight = 1
If 10 < Count < 20 then group all tags and give Weight = 2
If Count >= 20 then group all tags and give Weight = 3

How can I create this using LINQ?

Thanks,
Miguel

Re: Linq. Select by Pavel

Pavel
Fri Aug 29 02:27:37 CDT 2008

On Aug 28, 3:47=A0pm, shapper <mdmo...@gmail.com> wrote:
> I have 3 tables with the following fields:
>
> Posts: PostID, Title, Body
> Tags: TagID, Name
> PostsTags: PostID, TagID
>
> 1. I need to select all Tags (TagID and Name) that exist in PostsTags
> adding a field Count which holds the frequency of how often each tag
> is associated to a post, i.e., the number of times it shows in
> PostsTags.

from t in Tags
join pt in PostsTags on t.TagID equals pt.TagID into pts
let count =3D pts.Count()
where count !=3D 0
select new { t.TagID, t.Name, Count =3D count }

> 2. And would be also possible to group the tags as follows:
> =A0 =A0 If Count <=3D 10 then group all tags and give Weight =3D 1
> =A0 =A0 If 10 < Count < 20 then group all tags and give Weight =3D 2
> =A0 =A0 If Count >=3D 20 then group all tags and give Weight =3D 3

from t in Tags
join pt in PostsTags on t.TagID equals pt.TagID into pts
let count =3D pts.Count()
let weight =3D (count <=3D 10) ? 1 : (count >=3D 20) ? 3 : 2
where count !=3D 0
group new { t.TagID, t.Name, Count =3D count } by weight