如何使用内部联接编写Linq2Entities查询

时间:2009-11-19 02:23:47

标签: c# sql linq entity-framework linq-to-entities

如何从这些相关实体获取数据。我想只获取这些列: Term.Name,相关的Concept_Term.Weight,相关的Concept.Id

我编写了SQL,但我不想使用

   select t.Name,ct.ConceptId,ct.Weight from Term t 
   inner join Concept_Term ct on t.Id=ct.TermId
   inner join Concept c on c.Id=ct.ConceptId
   where c.Id == 80298 and t.LanguageId=2

我想看到的结果与控制台应用程序中的表格相同,结果与我在SQL中编写的结果相同。

实体图片:http://img7.imageshack.us/img7/7129/77365088.jpg

注意:抱歉,我无法在我的帖子中嵌入此照片,因为系统不允许我这样做。

2 个答案:

答案 0 :(得分:3)

如果您正确设置了关系,这很简单:

from t in db.Terms
where t.LanguageId == 2         // Do this early on for perf
from ct in t.ConceptTerms       // This is the reverse FK: ct.TermId -> t.Id
where ct.Concept.Id == 80298    // This is the other FK: ct.ConceptId -> c.Id
select new {
    t.Name, ct.ConceptId, ct.Weight
};

此代码假定您已将外键设置为双向工作。

它还表明您有一些冗余。而不是加入:

where ct.Concept.Id == 80298

我们可以直接进行检查:

where ct.ConceptId == 80298

答案 1 :(得分:0)

我找到了SQL In子句的解决方案,我从here开始实现 效果很好......

int[] conceptIdList = (from ct in db.Concept_Term
                               where ct.TermId == termId
                               select ct.ConceptId
                      ).ToArray();

         var result = db.Concept_Term
                      .Where(LinqExt.BuildOrExpression<Concept_Term, int>(ct => ct.ConceptId, conceptIdList))
                      .Select(ct => ct.Term.Name };

代码用于实现SQL IN(x,y,z)子句:

    public static Expression<Func<TElement, bool>> BuildOrExpression<TElement, TValue>(
        Expression<Func<TElement, TValue>> valueSelector, 
        IEnumerable<TValue> values
    )
{     
    if (null == valueSelector) 
        throw new ArgumentNullException("valueSelector");

    if (null == values)
        throw new ArgumentNullException("values");  

    ParameterExpression p = valueSelector.Parameters.Single();

    if (!values.Any())   
        return e => false;

    var equals = values.Select(value =>
        (Expression)Expression.Equal(
             valueSelector.Body,
             Expression.Constant(
                 value,
                 typeof(TValue)
             )
        )
    );
   var body = equals.Aggregate<Expression>(
            (accumulate, equal) => Expression.Or(accumulate, equal)
    ); 

   return Expression.Lambda<Func<TElement, bool>>(body, p);
}