[英]LINQ how to Filter the data based on the value of the properties
i have a problem with filtering data in LINQ , here is my Model : 我在LINQ中过滤数据时遇到问题,这是我的模型:
public class CoursePlan
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Semester { get; set; }
public string ModuleCode { get; set; }
public string ModuleName { get; set; }
public string Credits { get; set; }
public string OrderNumber { get; set; }
public string ModuleStatus { get; set; }
}
and here is my data Json
这是我的数据
Json
the problem here some modules having same OrderNumber
which mean they are optional , student must study one of them and if student already study one of them , i should ignore other modules in same order number. 这里的问题是某些模块具有相同的
OrderNumber
,这意味着它们是可选的,学生必须学习其中一个,如果学生已经学习了其中一个,则我应该忽略具有相同订货号的其他模块。
in other way to describe the question 用其他方式描述问题
i want to return a list of CoursePlan and on this list if there is two items having same OrderNumber
check the ModuleStatus
for each one of them and if any one is Completed
remove other modules on that order otherwise return them all . 我想返回CoursePlan的列表,如果有两个具有相同
OrderNumber
项目,请在此列表上检查每个模块的ModuleStatus
,如果其中任何一个已Completed
删除该订单上的其他模块,否则将它们全部退还。
here is my code 这是我的代码
var coursePlanList = await _sqLiteAsyncConnection.Table<CoursePlan>().ToListAsync();
var groupedData = coursePlanList.OrderBy(e => e.Semester)
.GroupBy(e => e.OrderNumber)
.Select(e => new ObservableGroupCollection<string, CoursePlan>(e))
.ToList();
for now im solving this by this algorithm and not sure if it's the best 现在我用这种算法解决了这个问题,不确定是否最好
var coursePlanList = await _sqLiteAsyncConnection.Table<CoursePlan>().ToListAsync();
List<CoursePlan> finalList = new List<CoursePlan>();
var counter = 0;
foreach (var itemPlan in coursePlanList)
{
if (counter > 0 && counter < coursePlanList.Count)
if (itemPlan.OrderNumber == coursePlanList[counter - 1].OrderNumber)
{
if (itemPlan.ModuleStatus == "Completed")
{
finalList.RemoveAll(a => a.OrderNumber == itemPlan.OrderNumber);
finalList.Add(itemPlan);
}
Debug.WriteLine(itemPlan.ModuleName + "With -->" + coursePlanList[counter - 1].ModuleName);
}
else
finalList.Add(itemPlan);
counter++;
}
var groupedData = finalList.OrderBy(e => e.ModuleStatus)
.ThenBy(e => e.Semester)
.GroupBy(e => e.Semester)
.Select(e => e)
.ToList();
CoursePlanViewList.BindingContext = new ObservableCollection<IGrouping<string, CoursePlan>>(groupedData);
Any advise or guidance would be greatly appreciated 任何建议或指导将不胜感激
Let me rephrase your requirement: you want to show all plans per OrderNumber
that meet the condition: none of the plans in their group should be "Completed" or the plans themselves should be "Completed". 让我重新
OrderNumber
您的要求:您要显示每个OrderNumber
满足条件的所有计划:组中的任何计划都不应该“完成”或计划本身应该“完成”。 All this grouped by Semester
: 所有这些按
Semester
分组:
var plansQuery =
from p in _sqLiteAsyncConnection.Table<CoursePlan>()
group p by p.Semester into sem
select new
{
PlansInSemester =
from p in sem
group p by p.OrderNumber into gp
select new
{
PlansInOrderNumber =
gp.Where(p => !gp.Any(p1 => p1.ModuleStatus == "Completed")
|| p.ModuleStatus == "Completed")
}
};
This gives you an IQueryable
that produces the course plans you want to select, but grouped in two levels, so the final result is obtained by flattening the query twice: 这为您提供了一个
IQueryable
,该IQueryable
生成了您想要选择的课程计划,但分为两个级别,因此,通过将查询展平两次可以得到最终结果:
var coursePlanList = await plansQuery
.SelectMany(x => x.PlansInSemester
.SelectMany(y => y.PlansInOrderNumber)).ToListAsync()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.