查询多个项目的列表 [英] Query a list for multiple items

查看:120
本文介绍了查询多个项目的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

  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);


这篇关于查询多个项目的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆