Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following:
C#
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
C#
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
Posted
Updated 25-Aug-16 5:39am

1 solution

Easy to do with LINQ:
C#
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:
C#
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);
 
Share this answer
 
Comments
Maciej Los 25-Aug-16 16:21pm    
Great, a5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900