开发者

How can i write Linq2Entities Query with inner joins

开发者 https://www.devze.com 2022-12-11 16:56 出处:网络
How can I get data from these related entities. I want to get these columns only: Term.Name , related Concept_Term.Weight, related Concept.Id

How can I get data from these related entities. I want to get these columns only: Term.Name , related Concept_Term.Weight, related Concept.Id

I wrote the SQL but I don't want to use

   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

What I want to see is the same result like a table in a console application with the same result that I wrote in SQ开发者_如何学编程L.

Picture of the entities : http://img7.imageshack.us/img7/7129/77365088.jpg

Note: Sorry I can't embed this photo in my post because the system doesn't allow me to do that.


If you've set up you relations properly, this is quite simple:

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
};

This code assumes you have set your foreign keys to work in both directions.

It also demonstrates that you have some redundancy. Instead of JOINing:

where ct.Concept.Id == 80298

We could do the check directly:

where ct.ConceptId == 80298


I found a solution for SQL In clause, which i take and implement from the here It works well...

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 };

Code is for implementing SQL IN (x,y,z) clause :

    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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消