我有一个像
这样的表格Application_OID
Status
{some other fields}
RecommendedFromApplication_OID
RecommendedFromApplication_OID将包含上次推荐的对象的Application_OID。例如,有一个应用程序
Application_OID = 1
Status = New
{some other fields}
RecommendedFromApplication_OID = null
建议将此应用程序用于其他作业,使用以下值创建另一个应用程序
Application_OID = 2
Status = New
{some other fields}
RecommendedFromApplication_OID = 1
并且第一个对象的状态将更新为
Application_OID = 1
Status = Recommended
{some other fields}
RecommendedFromApplication_OID = null
假设我推荐新创建的另一个作业,将使用以下值创建一个新条目
Application_OID = 3
Status = New
{some other fields}
RecommendedFromApplication_OID = 2
第二个应用程序将更新为推荐,最终数据将
Application_OID = 1
Status = Recommended
{some other fields}
RecommendedFromApplication_OID = null
Application_OID = 2
Status = Recommended
{some other fields}
RecommendedFromApplication_OID = 1
Application_OID = 3
Status = New
{some other fields}
RecommendedFromApplication_OID = 2
通过这种方式,我可以推荐一个我想要的应用程序。
现在问题;我想在LINQ中编写一个函数,它将获取Application_OID并返回包含所有推荐(子)应用程序的Application_OID的ID的List,如
private List<int> GetRecommendedIds(int id)
{
var applicationIds = (/*some query*/).ToList();
return applicationIds;
}
然后调用
GetRecommendedIds(1)
将返回值为
的int列表2,3
答案 0 :(得分:0)
public List<int> GetRecommendedForApplication(int Application_OID)
{
var recommendedApplications = new List<int>();
var context = new SomeEntities();
int applicationId = Application_OID;
Application application;
do
{
application = (from applications in context.Applications
join recommendedApplication in context.Applications
on applications.Application_OID equals
recommendedApplication.ParentApplication_OID
where recommendedApplication.ParentApplication_OID == applicationId
select recommendedApplication.Application_OID).FirstOrDefault();
if (application != null)
{
recommendedApplications.Add(application);
applicationId = application.Application_OID;
}
} while (application != null && application.ParentApplication_OID != null);
return recommendedApplications;
}