I want to merge, outer join the column "Count" from View_AssessmentCount list to View_Assessment. Of course Count will get an error because it is int and I wrote String.Empty. It's at this part where I specify where to outer join the null column "Count". But then again I don't know the right terms to Google this. Please help me expound on this.

public class AssessmentWithCount
{
    public int Count { get; set; }
    public String AssessmentName { get; set; }
    public String AssessmentInitials { get; set; }
    public Int16 id { get; set; }
}

public IEnumerable<AssessmentWithCount> GetAssessmentWithCount()
{
    using (var context = new SQL_TA_SCOREBOARDEntities1())
    {
        var query = from a in context.View_Assessment
                    join b in context.View_AssessmentCount on a.AssessmentName equals b.AssessmentName into ab
                    from subA in ab.DefaultIfEmpty()
                    select new AssessmentWithCount
                    {
                        AssessmentName = a.AssessmentName,
                        Count =  (subA == null ? String.Empty : subA.Count.ToString())
                    };
        return query;
    }
}

Updated post

Now my error is "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection." when I DataBind this class. :(

有帮助吗?

解决方案

Don't convert the database field to a string - just use it as-is:

Count = (subA == null ? 0 : (subA.Count ?? 0))

Regarding the error message... the using statement disposes your instance of SQL_TA_SCOREBOARDEntities1, so you need to get your data now, before leaving the method:

return query.ToList();

其他提示

Here try this instead:

Count = SubA.Count ?? 0

Just had some issues like these and got help. :) Sorry I can't comment as I have less than 50 reps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top