0

There is a problem I need help with your query on the data using LINQ to SQL and Entity Framework (I'm using Visual Studio 2010).

I have three tables:

  1. tblNewsDetails
  2. tblNewsCategories
  3. tblNewsInCategories

(See photo 1 in the picture below.)

Now, I want to retrieve records in the tblNewsDetails table, with condition CategoryId=1, as shown in photo 2 in the picture below.

But NewsID and CategoryId in tblNewsInCategories table are two foreign keys. I do not see them and I do not know how to use them in my code.

Also, my code has errors, shown in photo 3 in the picture below.

http://img.tamtay.vn/files/photo2/2010/5/28/10/962/4bff3a3b_1093f58f_untitled-1.gif

1
  • I really doubt you're using LINQ to SQL and Entity Framework at the same time... You're probably using Linq to Entities, not Linq to SQL Commented May 28, 2010 at 8:18

1 Answer 1

1

There's a couple approaches possible, here's one of em

from n in tblNewsInCategories.Include("NewsCategory").Include("NewsDetail")
where n.NewsCategory.CategoryID == 1
select n.NewsDetail

remember that n (and b, in your exemple) are TblNewsInCategories entities, which probably have the following properties :

{
    public int NewsInCategories { get; set; } // your middle table primary key
    public TblNewsCategory NewsCategory { get; set; } // a navigation property
    public TblNewsDetails NewsDetail { get; set; } // a navigation property
}

so in order to access NewsId and CategoryId, you have to go through the navigation properties.

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

1 Comment

Thank you very much Dynami Le-Savard. I have solved this problem. (I've lost three days to try to solve). You're a good person, and have good knowledge. I want to make friends with you on facebook :-)

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.