如何通过 EF6 将 SQLQuery 转换为 SortedList
How to convert SQLQuery to SortedList through EF6
我有一个名为 Materials
的 Entity Framework 6 class,它在我的数据库中反映为具有相同名称的 table。使用父参数,我需要 return 来自 SQL 查询的材料排序列表,以便我稍后可以检查用户所做的编辑不会影响顺序。我的 SQL 是一个如下所示的存储过程:
CREATE PROC [dbo].[GET_SortedMaterials](@FinishedGoodCode VARCHAR(50))
AS
SELECT
ROW_NUMBER() OVER (ORDER BY Component.Percentage_of_Parent DESC,Material.Material) AS _sortField
,Material.*
FROM
Components AS Component
INNER JOIN Materials AS Material ON Component.Child_Material = Material.Material
WHERE
Component.Parent_Code = @FinishedGoodCode
ORDER BY
Component.Percentage_of_Parent DESC
,Material.Material
如您所见,orderby
字段未包含在 Material
中。出于这个原因,我觉得我不能 return 只是一组 Material
对象并仍然保持排序 - 我已经在 SQL 中执行排序并添加 _sortField
(我认为该字段可能不是一个好主意)。
我读取 SQL 的 C# 代码如下所示:
public async Task<SortedList<int, Materials>> GET_SortedMaterials(IProgress<Report> progress, string finishedGoodCode)
{
try
{
var report = new Report { Message = "Retrieving Sorted Materials", NewLine = true, StatusCode = Enums.StatusCode.Working };
progress.Report(report);
using (var context = new DBContext())
{
var ingredientList = await context.Database.SqlQuery<(int _sortField,Materials mat)>("[app].[GET_Customers]").ToListAsync();
var sorted = new SortedList<int, Raw_Materials>();
foreach (var (_sortField, mat) in ingredientList.OrderBy(x=>x._sortField))
{
sorted.Add(_sortField, mat);
}
return sorted;
}
}
catch (Exception ex)
{ [EXCLUDED CODE]
}
}
当代码执行时,我得到了正确的行数 returned,但是我没有得到 Key
对应于 _sortField
值和Value
到 Material
值。我已经尝试了基本相同代码的各种不同版本,但我无法将脚本获取到 return 包含有关其排序信息的材料列表,相反,转换为 EF class 完全失败,我只能得到返回空值:
任何有关如何从 SQL return 排序列表并在 C# 中保持排序的建议,当排序字段不在 return 值中时,我们将非常感激.
使用
var ingredientList = await context.Database.SqlQuery<Materials>("[app].[GET_Customers]").Select((mat, _sortField) => (_sortField, mat)).ToDictionary(x => x._sortField, x => x.mat);
或者如果你想要异步加载使用
var ingredientList = await context.Database.SqlQuery<Materials>("[app].[GET_Customers]").ToListAsync().Result.Select((mat, _sortField) => (_sortField, mat)).ToDictionary(x => x._sortField, x => x.mat);
完整代码
public async Task<SortedList<int, Materials>> GET_SortedMaterials(IProgress<Report> progress, string finishedGoodCode)
{
try
{
var report = new Report { Message = "Retrieving Sorted Materials", NewLine = true, StatusCode = Enums.StatusCode.Working };
progress.Report(report);
using (var context = new DBContext())
{
var ingredientList = await context.Database.SqlQuery<Materials>("[app].[GET_Customers]").ToListAsync().Result.Select((mat, _sortField) => (_sortField, mat)).ToDictionary(x => x._sortField, x => x.mat);
var sorted = new SortedList<int, Raw_Materials>();
foreach (var item in ingredientList.OrderBy(x => x.Key))
{
sorted.Add(item.Key, item.Value);
}
return sorted;
}
}
catch (Exception ex)
{
[EXCLUDED CODE]
}
}
我有一个名为 Materials
的 Entity Framework 6 class,它在我的数据库中反映为具有相同名称的 table。使用父参数,我需要 return 来自 SQL 查询的材料排序列表,以便我稍后可以检查用户所做的编辑不会影响顺序。我的 SQL 是一个如下所示的存储过程:
CREATE PROC [dbo].[GET_SortedMaterials](@FinishedGoodCode VARCHAR(50))
AS
SELECT
ROW_NUMBER() OVER (ORDER BY Component.Percentage_of_Parent DESC,Material.Material) AS _sortField
,Material.*
FROM
Components AS Component
INNER JOIN Materials AS Material ON Component.Child_Material = Material.Material
WHERE
Component.Parent_Code = @FinishedGoodCode
ORDER BY
Component.Percentage_of_Parent DESC
,Material.Material
如您所见,orderby
字段未包含在 Material
中。出于这个原因,我觉得我不能 return 只是一组 Material
对象并仍然保持排序 - 我已经在 SQL 中执行排序并添加 _sortField
(我认为该字段可能不是一个好主意)。
我读取 SQL 的 C# 代码如下所示:
public async Task<SortedList<int, Materials>> GET_SortedMaterials(IProgress<Report> progress, string finishedGoodCode)
{
try
{
var report = new Report { Message = "Retrieving Sorted Materials", NewLine = true, StatusCode = Enums.StatusCode.Working };
progress.Report(report);
using (var context = new DBContext())
{
var ingredientList = await context.Database.SqlQuery<(int _sortField,Materials mat)>("[app].[GET_Customers]").ToListAsync();
var sorted = new SortedList<int, Raw_Materials>();
foreach (var (_sortField, mat) in ingredientList.OrderBy(x=>x._sortField))
{
sorted.Add(_sortField, mat);
}
return sorted;
}
}
catch (Exception ex)
{ [EXCLUDED CODE]
}
}
当代码执行时,我得到了正确的行数 returned,但是我没有得到 Key
对应于 _sortField
值和Value
到 Material
值。我已经尝试了基本相同代码的各种不同版本,但我无法将脚本获取到 return 包含有关其排序信息的材料列表,相反,转换为 EF class 完全失败,我只能得到返回空值:
任何有关如何从 SQL return 排序列表并在 C# 中保持排序的建议,当排序字段不在 return 值中时,我们将非常感激.
使用
var ingredientList = await context.Database.SqlQuery<Materials>("[app].[GET_Customers]").Select((mat, _sortField) => (_sortField, mat)).ToDictionary(x => x._sortField, x => x.mat);
或者如果你想要异步加载使用
var ingredientList = await context.Database.SqlQuery<Materials>("[app].[GET_Customers]").ToListAsync().Result.Select((mat, _sortField) => (_sortField, mat)).ToDictionary(x => x._sortField, x => x.mat);
完整代码
public async Task<SortedList<int, Materials>> GET_SortedMaterials(IProgress<Report> progress, string finishedGoodCode)
{
try
{
var report = new Report { Message = "Retrieving Sorted Materials", NewLine = true, StatusCode = Enums.StatusCode.Working };
progress.Report(report);
using (var context = new DBContext())
{
var ingredientList = await context.Database.SqlQuery<Materials>("[app].[GET_Customers]").ToListAsync().Result.Select((mat, _sortField) => (_sortField, mat)).ToDictionary(x => x._sortField, x => x.mat);
var sorted = new SortedList<int, Raw_Materials>();
foreach (var item in ingredientList.OrderBy(x => x.Key))
{
sorted.Add(item.Key, item.Value);
}
return sorted;
}
}
catch (Exception ex)
{
[EXCLUDED CODE]
}
}