【发布时间】:2018-01-22 12:45:04
【问题描述】:
我目前有这个数据集
所以,我试图根据类别 ID 作为输入返回数据。但是如果该类别没有关键字,则应返回其父级的数据
这是一个例子
输入:202;输出:ParentCategoryID=201,名称=操作系统, 关键字=教学
这就是我现在拥有的(testData 是数据集)
public static string GetData(int categoryId)
{
var searchResult = testData.Where(x => x.CategoryId == categoryId).FirstOrDefault();
//if has no keywords return parent data
if(string.IsNullOrEmpty(searchResult.Keywords))
{
var parentData = testData.Where(x => x.CategoryId == searchResult.ParentCategoryId);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("ParentCategoryId=");
stringBuilder.Append(parentData.ParentCategoryId);
stringBuilder.Append(", Name=");
stringBuilder.Append(parentData.Name);
stringBuilder.Append(", Keywords=");
stringBuilder.Append(parentData.Keywords);
return stringBuilder.ToString();
}
但这仅适用于一位家长。它适用于这种场景
输入:201;输出:ParentCategoryID=200,名称=计算机, 关键字=教学
我认为我的 LINQ 查询存在问题,我将不胜感激
【问题讨论】:
-
听起来你需要循环或递归