Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a LINQ to Entities query including both tag class name and text string
    text
    copied!<p>My ASP.NET MVC project has some</p> <blockquote> <p>chapter class</p> </blockquote> <p>and related </p> <blockquote> <p>tag class</p> </blockquote> <p>They have many to many relationship though one tag has multiple chapter and also one chapter has multiple tags on it.</p> <p>My question is: I want to make a query by using <code>tag.title</code> within this structure but LINQ doesn't allow objects; it only gets variables like <code>string</code>, <code>int</code> ...etc.</p> <p>I am feeling a little bit stupid but I can't figured it out this basic thing :( (May be I have to give a break to let it be clear in my mind)</p> <p>My models are:</p> <pre><code>public partial class Chapter { public string Id { get; set; } public string Subject { get; set; } public string Content { get; set; } public virtual ICollection&lt;Tag&gt; Tags { get; set; } } public partial class Tag { public int? TagId { get; set; } public string Title { get; set; } public int CallCount { get; set; } public virtual ICollection&lt;Chapter&gt; Chapters { get; set; } } </code></pre> <p>and also for db:</p> <pre><code>public class DataContext : DbContext { public DbSet&lt;Chapter&gt; Chapters { get; set; } public DbSet&lt;Tag&gt; Tags { get; set; } } </code></pre> <p>so here we go:</p> <pre><code>var tag = (from t in db.Tags where t.Title.Contains(search_word)).FirstOrDefault(); var chapters = (from c in db.Chapters where (m.Title.Contains(search_word) || m.Tags.Contains(tag)) select c).ToList(); </code></pre> <p>As I explained above; it gives </p> <blockquote> <p>"you should use string, guid, int... on queries"</p> </blockquote> <p>error for this query. I think there is a logical failure I made but my brain stopped responding...</p> <p>Briefly; I want to search by tag names and if you have another approach I will be pleased to listen it..</p> <p>Any help will be highly appreciated.</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload