1

I am stuck here with Linq query... I am just trying to get my hands on MVC via this very simple blog application.. I am hving these three entities

Post, AuthorDetails, CommentsDetails, //* in future will be adding categories, tags

i want to search using the searchterm passed into the methods, which will then search for that searchString in Post.Title, Post.Body,AuthorDetails.FirstName,AuthorDetails.LastName,CommentsDetails.Comments and waht to return something which i can cast to List<> ...please have a look what i have got so far.

Code..

 public List<Post> GetPostBySearchItem(string searchString)
        {
            List<Post> getAllPostsBySearchString = (from p in ePost.Posts
                                                    join a in ePost.AuthorDetails
                                                    on p.AuthorId equals a.Id
                                                    join c in ePost.CommentsDetails
                                                    on a.Id equals c.Id
                                                    where p.Title.Contains(searchString) || p.PostBody.Contains(searchString) || a.FirstName.Contains(searchString) || c.Comments.Contains(searchString)
                                                    select p).ToList();
            return getAllPostsBySearchString;

2 question here 1) whether the join statements are correct in this code 2) and how can i return something like this select P for Posts, A for AuthoreDetails and c for CommentsDetails...

3
  • 1
    Not an answer to your question directly, but it seems like you are trying to add search capability to your site... I would highly recommend looking at Lucene for .Net to add such functionality. You can find an example here ifdefined.com/blog/post/2009/02/… Commented Sep 7, 2011 at 22:10
  • Is there a reason you're using joins rather than associations here? If you establish associations from Post to Comments/Authors/Categories/etc, you could return List<Post> and navigate to the children in your view assuming you eager load the children appropriatly. Sounds like you're going through much of the same thing I did with www.ThinqLinq.com. You're free to check out my source in the file downloads at link. Commented Sep 8, 2011 at 15:25
  • @jim: i will surely have a look at it... :) Commented Sep 8, 2011 at 17:21

1 Answer 1

2

Your question #2: you simply have to return with "new" like this:

select new { Post = p, AuthorDetail = a, CommentDetail = c}).ToList()

instead of:

select p).ToList();

Look up Anonymous Type

Sign up to request clarification or add additional context in comments.

4 Comments

Thx... i have tried that and tried again ...its simply giving me the error " Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<MvcMBlog1.Models.Post>'
the problem is here.. what should i do here .... List<Post> getAllPostsBySearchString .. i have also tried var.. just to let you know...
Yes, because your return type is List<Post>. According your question was "how can I return something like..." that would be anonymous type, and you can't return a Post. The only other way you can do it is top have the AuthorDetail and CommentDetail within the Post object. Then your return type will work. Check the example: msdn.microsoft.com/en-us/library/bb425822.aspx
thats great... works fine... just to confirm my LINQ joins are fine ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.