gpt4 book ai didi

c# - 第 2 组类似请求 Entity Framework/Linq in 1

转载 作者:太空宇宙 更新时间:2023-11-03 10:25:39 24 4
gpt4 key购买 nike

我有 2 个具有相同列的 SQL Server View 。我有 2 个与 Entity Framework 完全相同的 linq 请求;一个应用于 View1,另一个应用于 View2。我想在一次调用中将 2 个请求分组。

switch (day) {
case day == MONDAY:
List<int> results = (from day in **container.View1**
join model in container.OtherTable ...
where ...
...
select id).ToList();
break;
case day == FRIDAY:
List<int> results = (from day in **container.View2**
join model in container.OtherTable ...
where ...
...
select id).ToList();
break;
}

我想要这样的东西

ObjectSet<EntityObject> cont= null;

switch (day) {
case day == MONDAY:
cont = container.View1;
break;
case day == FRIDAY:
cont = container.View2;
break;
}

List<int> results = (from day in **cont**
join model in container.OtherTable ...
where ...
...
select id).ToList();

优化代码,但我不知道如何将这些代码分组......

最佳答案

让您想要从中选择的所有不同 View 实现相同的界面并执行类似的操作:

public class Something {
public List<int> GetSomething(Day day)
{
var dbc = new Context();

switch (day)
{
case MONDAY: return GetResult(dbc.table1, dbc );
case FRIDAY: return GetResult(dbc.table2, dbc);
}
}

public List<int> GetResult<T>(DbSet<T> table, Context context) where T : class, MyInterface
{
List<int> results = (from day in table
join model in context.OtherTable on day.Id equals model.dayId
select day.Id).ToList();
return results;
}
}

public interface MyInterface
{
int Id { get; set; }
}

编辑:至于你的评论,创建一个像上面那样的接口(interface),并让你的实体实现它。

关于c# - 第 2 组类似请求 Entity Framework/Linq in 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31678035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com