user1841243
user1841243

Reputation: 1713

convert sql to linq with two tables

select ind.desc,ind.number
from int_goals_df idd, goals_df ind
where idd.dld_number = 123456
and ind.number = idd.ind_number
and ind.categorie = 2
order by follownumber

I'm having a hard time translating this to linq since it is using two tables. I've currently solved this now imperatively with a foreach loop but not happy with it..

I'm trying to get a list of goals_df that matches with a list of int_goals_df.

Any tips would be greatly appreciated ! Thank you !

EDIT - here is the code I'm using:

//get current GoalDefinitions by selected Goal
var currentGoalDefinition = MyAppAppContext.MyAppAppContextInstance.MyAppContext.GoalDefinitions.FirstOrDefault(
    d => d.DLD_GoalDFID == interv.Goal.DLD_GoalenDFID);

// get current intervGoalDefinitions by GoalDefinition
var currentintervGoalDefinitions = MyAppAppContext.MyAppAppContextInstance.MyAppContext.intervGoalDefinitions.Where(
    idd => idd.DLD_GoalDFID == currentGoalDefinition.DLD_GoalDFID).OrderBy(idd => idd.IDD_VolgNummer);

intervDefinitionCollection = new ObservableCollection<intervDefinition>(MyAppAppContext.MyAppAppContextInstance.MyAppContext.intervDefinitions.Where(i => i.IND_Categorie == intCategorie));

// filter intervGoalDefinitions by intervDefinitions
var intervDefinitionCollectionTemp = new ObservableCollection<intervDefinition>();
foreach (var currentintervGoalDefinity in currentintervGoalDefinitions)
{

    var foundintervGoalDefinitySorted = intervDefinitionCollection.FirstOrDefault(
        i => i.IND_intervDFID == currentintervGoalDefinity.IND_intervDFID);

    if (foundintervGoalDefinitySorted != null)
    intervDefinitionCollectionTemp.Add(foundintervGoalDefinitySorted);
}

intervDefinitionCollection = intervDefinitionCollectionTemp;

Upvotes: 1

Views: 97

Answers (4)

Hamid Pourjam
Hamid Pourjam

Reputation: 20744

context.int_goals_df.Join(context.goals_df, x => x.ind_number, x => x.number,
    (x, y) => new
    {
        idd = x,
        ind = y
    })
    .Where(x => x.idd.dld_number = 123456 && x.ind.categorie = 2)
    .OrderBy(x => x.idd.follownumber)
    .Select(x => new
    {
        x.ind.desc,
        x.ind.number
    });

Upvotes: 1

James
James

Reputation: 9965

I tend to use the sql syntax without implicit joins

/*Fields*/
    SELECT ind.desc, ind.number
/*Tables*/
    FROM int_goals_df idd
    INNER JOIN goals_df ind
    ON ind.number = idd.ind_number
/*Conditions*/
    WHERE idd.dld_number = 123456
    AND ind.categorie = 2
/*Order/Grouping*/
    ORDER BY follownumber

You can see from Chris's answer this translates more easily to linq.

Upvotes: 0

Chris McKelt
Chris McKelt

Reputation: 1388

quick go - think you need the join

var results = from idd in session.Query<int_goals_df>()
          join ind in session.Query<goals_df>()
          on   idd.ind_number equals ind.ind_number
          where idd.DlDNumber = 123456 && idd.Category.Id == 2
          orderby idd.FollowNumber
          select new { idd.Description, idd.Number };

Upvotes: 0

Firo
Firo

Reputation: 30803

assuming NHibernate as ORM and int_goal is a subclass of goal

var results = from idd in session.Query<IntGoals>()
              where idd.DlDNumber = 123456 && idd.Category.Id == 2
              orderby idd.FollowNumber
              select new { idd.Description, idd.Number };

Upvotes: 1

Related Questions