我在VS 2015中使用Entity Framework工作。 我有两个名为Formulas和Input_Field_Formulas的表。 在我的一种方法中,我想得到一个类型公式的可观察集合给出搜索标准。 但结果列表应按某种排序顺序排列。 不幸的是,该排序顺序的列位于表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;
提前致谢!
答案 0 :(得分:1)
尝试这种方式:
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());