473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Filter

Hello,

I have 3 tables:
Posts (PostID, ...)
PostsTags (PostID, TagID)
Tags (TagID, Name)

PostsTags associate each post to one or many tags.

I need to select all posts associated to a tag given the tag's name.
If the given tag's name is null then return all posts.

I have the following:

posts = (from p in database.Posts
let tags = p.PostsTags.Where(pt =pt.Tag.Name == tag ||
tag == null).Select(pt =pt.Tag).DefaultIfEmpty()
orderby p.UpdatedAt descending
select new PostLeaf {
Post = p,
Tags = tags.ToList(),
TagsCSV = string.Join(", ", tags.Select(t =>
t.Name).ToArray())
}).ToPagedList(page.HasValue ? page.Value - 1 : 0,
PageSize);

When I run this code with a certain tag what I get ALL posts. They are
not filtered.
However, all the posts that are not associated to that tag their
TagsCSV property is empty.

What am I missing here?

Thanks,
Miguel
Nov 18 '08 #1
1 1519
On Nov 18, 5:20*pm, shapper <mdmo...@gmail.comwrote:
Hello,

I have 3 tables:
Posts (PostID, ...)
PostsTags (PostID, TagID)
Tags (TagID, Name)

PostsTags associate each post to one or many tags.

I need to select all posts associated to a tag given the tag's name.
If the given tag's name is null then return all posts.

I have the following:

*posts = (from p in database.Posts
* * * * * * *let tags = p.PostsTags.Where(pt =pt.Tag.Name == tag ||
tag == null).Select(pt =pt.Tag).DefaultIfEmpty()
* * * * * * *orderby p.UpdatedAt descending
* * * * * * *select new PostLeaf {
* * * * * * * * *Post = p,
* * * * * * * * *Tags = tags.ToList(),
* * * * * * * * *TagsCSV = string.Join(", ", tags.Select(t =>
t.Name).ToArray())
* * * * * * * }).ToPagedList(page.HasValue ? page.Value - 1: 0,
PageSize);

When I run this code with a certain tag what I get ALL posts. They are
not filtered.
However, all the posts that are not associated to that tag their
TagsCSV property is empty.

What am I missing here?

Thanks,
Miguel
I made it work as follows:

let tags = p.PostsTags.Select(pt =>
pt.Tag).DefaultIfEmpty()
orderby p.UpdatedAt
descending
where p.PostsTags.Where(pt =pt.Tag.Name ==
tag || tag == null).Count() != 0

I think this is ok.

Thanks,
Miguel
Nov 18 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a variable length list of associated values after...
1
by: Robert Neville | last post by:
I would like to add filter functionality to my database whether through the Main form or the subform. This question may be rudimentary, yet I have not less experience with filtering data outside...
0
by: CSDunn | last post by:
Hello, I have a problem with field filtering between an Access 2000 Project form (the application is called CELDT), and the report that shows the results of the filter. Both the form and the...
3
by: Richard | last post by:
Hi, I have a form based on a table. When I filter the form I want to run a report based on the same table with the same filter as the form. No problem until I want to filter a combo box where...
8
by: dick | last post by:
I am just trying to print/report the results of a "filter by selection" which is done by right-clicking a form, filling in values, and "applying the filter." I have searched the newsgroups, and...
2
by: Salad | last post by:
I have a log file with a list of records. The log file can be unfiltered or filtered. I have a command button to call a data entry form from the log. At first I was only going to present the...
2
by: Mike Sweetman | last post by:
I have a form Form1 which when the Advanced Filter/Sort is used creates a form(maybe) with a title 'Form1Filter1 : Filter'. When I apply the filter to Form1 it is applied, but the value of...
8
by: marcus.kwok | last post by:
I am having a weird problem and I have can't figure out why it is happening. I create an OpenFileDialog and set a filename filter. When the dialog first opens, the filter works correctly, and...
15
by: Briansmi1116 | last post by:
I created two buttons, that filter my form, They filter in different Fields, and if one is filtered, and the other is not there is a certain amount of record, if they are both pushed, then there is...
1
by: woodey2002 | last post by:
Hi Everyone and many thanks for your time.. I am trying to begin access and a bit of VBA i am enjoying it but I have a annoying problem I just can’t get any where on. My databse mostly includes...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.