1

I am getting data from these two tables using linq to entities, relationships exist between tables on primary foriegn key basis, result set is coming but every row is repeating multiple times in reult however in Db there are not duplicate rows. Don't understand how to resolve this.

here is piece of code:

StringBuilder sb = new StringBuilder();
        string text = txtBoxSearch.Text;
        OLSContainer ols = new OLSContainer();
        var result = from tex in ols.COURSEs
                     from another in ols.UNITs
                     where tex.courseName.Contains(text) || tex.description.Contains(text) || another.unitName.Contains(text)
                     select new { tex,another };

        foreach (var cours in result)
        {
            sb.AppendLine("<h2 id='" + cours.tex.courseID + "'><a href='admin.aspx?id='" + cours.tex.courseID + "''>" + cours.tex.courseName + "</a></h2>");
        }

        foreach (var cours in result)
        {
            sb.AppendLine("<h2 id='" + cours.another.unitID + "'><a href='admin.aspx?id='" + cours.another.unitID + "''>" + cours.another.unitName + "</a></h2>");
        }
1
  • What does the SQL query look like in the profiler? Also, what's the full relationship between courses and units? Is it a one-to-many? Commented Jan 4, 2013 at 14:56

1 Answer 1

1

The problem is this:

var result = from tex in ols.COURSEs
             from another in ols.UNITs

It is a cross join. It matches every course with every unit. It doesn't use any FK/PK because no relation (navigation property) is used in this query. To use the relation you have to modify it to:

var result = from tex in ols.COURSEs
             from another in tex.SomeNavigationProperty  // tex 
Sign up to request clarification or add additional context in comments.

2 Comments

What is the solution how to overcome this.. i have PK, FK relationship in EDM.
You must query the relationship not the entity set directly. If you query the entity set directly you must do manual join.

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.