查询多个项目的列表

编程入门 行业动态 更新时间:2024-10-23 23:23:38
本文介绍了查询多个项目的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下内容:

public class DeviceListItem { public DeviceListItem(); public string AssignedLot { get ; set ; } public string ID { get ; set ; } }

该列表可以包含 1.只有一项例如ID-001和Lot-AA 2.具有不同ID但常见批次的多个项目,例如带有LOT-AA的ID-001和带有LOT-AA的ID-002 3.具有不同ID和不同批次的多件物品,例如带有LOT-AA的ID-001和带有LOT-BB的ID-002 我也有以下函数返回一个字符串

private string generateErrorMessage(List< DeviceListItem> mixedDeviceList) { string error = string .Empty; // 如果只有1个设备 - 设备[< ID>]不属于此批次。它属于lot [< Lot>]。 if (mixedDeviceList.Count == 1 ) error = Device + mixedDeviceList [ 0 ]。ID + 不属于此批次。它属于批次 + mixedDeviceList [ 0 ]。AssignedLot; // 多个,同一批次 - 来自批次的< n>设备[ < Lot>]与当前批次混合。 // 多个,多个 - 有多个批次中的&n;设备与当前批次混合在一起。 return 错误; }

我需要查询列表并为上面标注的代码构建相应的字符串。 任何解决方案? 我的尝试: 尝试使用Linq做一些事但没有成功

解决方案

使用LINQ很容易:

if (mixedDeviceList.Count == 1 ) { return string .Format( 设备{0}不属于此批次。它属于批次{1}。,mixedDeviceList [ 0 ]。ID,mixedDeviceList [ 0 ]。AssignedLot); } string firstLot = mixedDeviceList [ 0 ]。AssignedLot ; bool multipleLots = mixedDeviceList.Skip( 1 )。任意(d = > d.AssignedLot!= firstLot); if (multipleLots) { return string .Format( 有{0}个设备来自与当前批次混合的多个批次。,mixedDeviceList.Count); } return string .Format( 批次[{1}]中有{0}个设备与当前批次混合。,mixedDeviceList.Count, firstLot);

或者,如果您更愿意避免使用LINQ:

if (mixedDeviceList.Count == 1 ) { return string .Format( Device {0}不属于这个批次。它属于批次{1}。,mixedDeviceList [ 0 ]。ID,mixedDeviceList [ 0 ] AssignedLot)。 } string firstLot = mixedDeviceList [ 0 ]。AssignedLot ; bool multipleLots = false ; for ( int index = 1 ; index < mixedDeviceList.Count; index ++) { if (mixedDeviceList [index] .AssignedLot!= firstLot) { multipleLots = true ; break ; } } 如果(multipleLots) { return string .Format( 有多个批次中的{0}个设备与当前批次混合。,mixedDeviceList.Count); } return string .Format( 批次[{1}]中有{0}个设备与当前批次混合。,mixedDeviceList.Count, firstLot);

I have the following:

public class DeviceListItem { public DeviceListItem(); public string AssignedLot { get; set; } public string ID { get; set; } }

The list can contain 1. Only one item such as ID-001 and Lot-AA 2. Multiple items with different ID's but common Lots, such as ID-001 with LOT-AA and ID-002 with LOT-AA 3. Multiple items with different ID's and different Lots, such as ID-001 with LOT-AA and ID-002 with LOT-BB I also have the following function that returns a string

private string generateErrorMessage(List<DeviceListItem> mixedDeviceList) { string error = string.Empty; // If only 1 device - "Device [<ID>] does not belong to this lot. It belongs to lot [<Lot>]." if (mixedDeviceList.Count == 1) error = "Device" + mixedDeviceList[0].ID + " does not belong to this lot. It belongs to lot " + mixedDeviceList[0].AssignedLot; // Multiple, same Lot - "There are <n> devices from lot [<Lot>] mixed with the current lot." // Multiple, multiple - "There are <n> devices from multiple lots mixed with the current lot." return error; }

I need to query the list and build the appropriate string to be returned for the remarked code above. Any solutions ? What I have tried: Tried to do something using Linq but no sucess

解决方案

Easy to do with LINQ:

if (mixedDeviceList.Count == 1) { return string.Format("Device {0} does not belong to this lot. It belongs to lot {1}.", mixedDeviceList[0].ID, mixedDeviceList[0].AssignedLot); } string firstLot = mixedDeviceList[0].AssignedLot; bool multipleLots = mixedDeviceList.Skip(1).Any(d => d.AssignedLot != firstLot); if (multipleLots) { return string.Format("There are {0} devices from multiple lots mixed with the current lot.", mixedDeviceList.Count); } return string.Format("There are {0} devices from lot [{1}] mixed with the current lot.", mixedDeviceList.Count, firstLot);

Or, if you'd prefer to avoid LINQ:

if (mixedDeviceList.Count == 1) { return string.Format("Device {0} does not belong to this lot. It belongs to lot {1}.", mixedDeviceList[0].ID, mixedDeviceList[0].AssignedLot); } string firstLot = mixedDeviceList[0].AssignedLot; bool multipleLots = false; for (int index = 1; index < mixedDeviceList.Count; index++) { if (mixedDeviceList[index].AssignedLot != firstLot) { multipleLots = true; break; } } if (multipleLots) { return string.Format("There are {0} devices from multiple lots mixed with the current lot.", mixedDeviceList.Count); } return string.Format("There are {0} devices from lot [{1}] mixed with the current lot.", mixedDeviceList.Count, firstLot);

更多推荐

查询多个项目的列表

本文发布于:2023-11-22 20:48:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618965.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   项目   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!