.NET Framework - Linq to Sql query help

Asked By ale
06-Jun-08 01:22 PM
I have the following database schema:

posts
-------
post_id
post_name


posts_tags
-------------
poast_id
tag_id


tags
-------
tag_id
tag_value


I need to select all posts records for provided tags. For example, give me
all posts that have tags "c#, asp.net, microsoft", or give me all posts that
have tags "sql, linq" etc.

I'm having trouble comming up with a Linq query to do that.

Thanks for your help,
Alex
LLBLGen
(1)
GroupBy
(1)
ToList
(1)
TextBox1.Text.Split
(1)
Database
(1)
Check
(1)
Split
(1)
Show
(1)
  Frans Bouma [C# MVP] replied...
07-Jun-08 03:23 AM
I don't have a lot of time so I can't dig deeper in this query, but
it's indeed not something that's really easy. In general, the results
are the same as: get all posts which aren't in the list of all posts
which don't have one of the tags.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
  Chris Jobson replied...
07-Jun-08 05:58 PM
I'm no expert but I've come up with something that seems to work, but
BEWARE - I've no idea how efficient it would be with large data sets
(probably not very!). However, it might give you some ideas. I've split it
up into a number of queries as it makes it easier to see what's going on,
but it's probably possible to combine them. Apologies if you prefer the
(and apologies too for the very unhelpful names I''ve used).

// Get sequence of tags wanted with no duplicates
var tagNames = textBox1.Text.Split(',').ToList().Distinct();
int numOfTags = tagNames.Count();

// Get sequence of ids of wanted tags (may want to do case-insensitive
comparisons here)
var tagids = tagNames.Join(tags, tn => tn, t => t.tag_value, (tn, t) =>
t.tag_id);

// Check that all tags are known
if (tagids.Count() != numOfTags)
{
MessageBox.Show("Not all tags were recognised");
return;
}

// Effectively get all post_tag entries for all the required tags and group
them by the post id
var posttags =
post_tags.Join(tagids, pt => pt.tag_id, t => t, (pt, t) => new {
pt.post_id, pt.tag_id}).
GroupBy(x => x.post_id);

// Each posttags entry consists of a Key (post_id) and a collection of tag
ids. We only want
// the ones with ALL the required tag ids.
var postids = posttags.Where(pts => pts.Count() == numOfTags).Select(pts =>
pts.Key);

// Finally convert the post ids into names
var postnames = postids.Join(posts, pi => pi, p => p.post_id, (pi, p) =>
p.post_name);

Good luck!
Chris Jobson
Create New Account
help
RL public List<MyTable> GetMyTablesByName(string aString) { List<MyTable> myMyTableList = new List<MyTable> (); try { DataContext database = new DataContext(MyConnectionString); var MyMatchingTables = from X in db.MyTables where X.AField.StartsWith(aString never null? Must be built into Linq to Sql to never return null? myMyTableList = MyMatchingTables.ToList(); } catch (Exception ex) { Debug.WriteLine(ex.Message); / / never triggered, even if no match } return myMyTableList That's what is coming out of the query even if you did not use ToList(), an empty List<T> being created. Now if you were using First() or FirstorDefault(), the can be used to query, project and filter data in arrays, enumerable classes, XML, relational database, and third party data sources. (* ** ** While it allows any data source to be queried, it a different format to be executed on a separate data store (such as on a database server as SQL queries). (* ** ** The results of a query are returned as a collection of 20xx it will come out). RL Just for absolute clarity . . . a List is created when ToList() is invoked. A List<T> gets created when ToList<T> () is invoked. The FirstOrDefault method may return null because null is a perfectly valid more than one object is being returned out of the query, you do not need ToList() it is going to return a List<T> by itself of objects in a tubular
for table .NET Framework Hi, How do I check if a table exists in my database? - - Thanks Morris ASP.NET Web Controls Discussions SQL Server (1) CALLMasterMDB (1) OleDbConnection.GetSchema (1 TABLES Check the result you can see all the base tables and views of the database. Please let me know if it works. If you have further questions please feel free column to be visible if a table (mailboxactivitylog) exists in either the sql or access database bound to the gridview via datasource. How do I check if this table exists? I correct you have two questions: 1. How to know if a tables exists in the database. 2. How to hide a column of a GridView manually. To the first question, we can query the database to see all the tables in it. Then check if the table exists in the database. Generally database has some system tables that can help to do this. For SQL Server, it's So we can use the following query: SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES For Access database we use MSysObjects. Here's the query for Access: Select Name from MSysObjects We can
oop style syntax goes. What are you thoughts? Cheers, Michael C# Discussions Visual Studio (1) LLBLGen (1) ASP.NET (1) LINQ (1) SequenceEqual (1) IEnumerable (1) Database (1) Reverse (1) To which book are you referring? Why is using the linq keywords _strictly_ set-based doesn't make sense to me either, since when you query the database, records always have to be returned in _some_ order. Even if the database doesn't impose a specific order on the data, it still has some inherent order undefined order, unless you specify the ordering. So running the same unordered query on a database can result in different orderings. the consumption of the set you see returned by the queries a conversion has to take place which isn't obvious. FB - - - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - Lead developer of LLBLGen Pro, the productive O / R mapper for .NET LLBLGen Pro website: http: / / www.llblgen.com My .NET blog: http: / / weblogs.asp.net / fbouma Microsoft MVP (C#) - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - On Thu, 04
ORM .NET Framework The though of writing tons of Database code for a big project sends me to sleep. I've used Java's EJB It is however going to be a very large multi-user system, with a local database cache, clients that connect to this using remoting. I am planning to make use of for a large, real world system? Marc .NET Framework Discussions Entity Framework (1) NHibernate (1) LLBLGen (1) VB.NET (1) LINQ (1) GroupJoin (1) Subscribe (1) Database (1) I cannot answer your question about nHibernate. I just wanted to suggest that you http: / / csharpindepth.com I wouldn't hold my breath for that. FB - - - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - Lead developer of LLBLGen Pro, the productive O / R mapper for .NET LLBLGen Pro website: http: / / www.llblgen.com My .NET blog: http: / / weblogs.asp.net / fbouma Microsoft MVP (C#) - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - On Jul 29 cannot answer to nHibernate. One thing I would look at, if I were you, is LLBLGen Pro (http: / / www.llblgen.com). It is at a good price for scale. Plus the
many thanks! Is there an elegant maner to do the same with this code : words = TextBox1.Text.Split(New String() {" "c, ", "c, "."c, ":"c, I tried this with the idea i could use the array chars already done: words = TextBox1.Text.Split(New String() {chars}, StringSplitOptions.RemoveEmptyEntries) but it does not work! The aim of the code