我正在VS 2015与实体框架一起工作。我有两个表,叫做公式和Input_Field_Formulas。在我的一种方法中,我想得到一个类型公式的观察集,以给出搜索准则。但结果列表将按某种排序顺序排列。不幸的是,该sortorder的列位于表Input_Field_Formulas中。
我试着用LINQ来做,但这不管用。我想按降序得到公式,但当它们保存在数据库中时,我按顺序得到它们。
public ObservableCollection<Formulas> GetSelectedFormulas(int inputFieldId)
{
ObservableCollection<Formulas> formulasList = null;
try
{
using (paragraph16Entities db = new paragraph16Entities())
{
formulasList = new ObservableCollection<Formulas>(db.Formulas.Join(db.Input_Field_Formulas
.Where(x => x.input_field_id == inputFieldId).OrderByDescending(x => x.sort_order),
a => a.formula_id,
b => b.formula_id,
(a, b) => new { Formulas = a, Input_Field_Formulas = b })
.Select(a => a.Formulas)
.ToList());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return formulasList;
}我如何更改上面的LINQ或将如下所示的SQL转换为LINQ?
SELECT Formulas.formula_id, Formulas.formula_name
FROM Formulas
JOIN Input_Field_Formulas ON Input_Field_Formulas.formula_id = Formulas.formula_id
WHERE Input_Field_Formulas.input_field_id = 49
ORDER BY Input_Field_Formulas.sort_order;提前感谢!
发布于 2017-04-20 09:10:26
试着这样做:
formulasList = new ObservableCollection<Formulas>(db.Formulas.Join(db.Input_Field_Formulas
.Where(x => x.input_field_id == inputFieldId),
a => a.formula_id,
b => b.formula_id,
(a, b) => new { Formulas = a, Input_Field_Formulas = b })
.OrderByDescending(x => x.Input_Field_Formulas.sort_order)
.Select(a => a.Formulas)
.ToList());https://stackoverflow.com/questions/43514778
复制相似问题