Click here to Skip to main content
14,693,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Two Table Process And orderDetails.
now I need get process which contain all costCenterID from order Details list.
List<Process > process= new List<Process >()
 {
  new Process{Id=1, Name="Process 01", CostCenter="1,2,3,4" , companyID = 1  },
  new Process{Id=2, Name="Process 02", CostCenter="1,4,10"  , companyID = 1  },
  new Process{Id=3, Name="Process 03", CostCenter="1,2,5"   , companyID = 1  },
    }

 List<orderDetails> orderDetails= new List<orderDetails>()
 {
   new orderDetails{ Id=1, Name="order 01", CostCenterId ="1" , companyID = 1},
   new orderDetails { Id=2, Name="order 02", CostCenterId="2" , companyID = 1},
 }

I've tried this code but it didn't work , Note used linq

var orderDetails = db.OrderDetails.Where(c => c.OrderId == order.Id);
    var orderCostCenter = orderDetails.Select(c => c.CostCenterId).ToArray();
    var listProcess = process.where (c=>c.companyID  == 1 &&
                  c.CostCenter.split(',').containt(orderCostCenter)).firstorDefault();

the linq query result return two columns=
new Process{Id=1, Name="Process 01", CostCenter="1,2,3,4" , companyID = 1  },
  new Process{Id=3, Name="Process 03", CostCenter="1,2,5"   , companyID = 1  },


What I have tried:

I'm tired to find and resolve this issue, please help me.
Posted
Updated 19-May-20 2:29am
v2
Comments
BillWoodruff 19-May-20 1:58am
   
What is wrong with the current output ?

Please show an example of what the correct output would be.

Suggestion: consider using a Flags Enum for 'CostCenter instead of an array of strings to simplify parsing.
Waleed Abukatab 19-May-20 2:36am
   
i need this output using linq :
{Id=1, Name="Process 01", CostCenter="1,2,3,4" , companyID = 1 },
{Id=3, Name="Process 03", CostCenter="1,2,5" , companyID = 1 },


That Linq doesn't compile: you are missing semicolons, "where" should be "Where", orderDetails doesn't contain an OrderId (or you don't set it's value at all), "split" should be "Split", "containt" should be "Contains", and "firstorDefault" should be "FirstOrDefault". And you definitely shouldn't be creating your own class called "Process" ...

Which means that the Linq you show is useless as it hasn't been tested by you, and thus doesn't produce any results, much less those you loosely describe.

And if you haven't tested it, neither can we - so we have no idea what your actual inputs look like, or what outputs you expect.

Show us actual copy'n'paste code that compiles, show us the actual input data, show us the actual output, and the expected output.

And do yourself a favour: C# is rich in collections, so why store any kind of list as comma delimited? Instead, use a collection of references to actual CostCenter items, and your life become a lot easier anyway ...
   
Comments
BillWoodruff 20-May-20 1:50am
   
+5

The trick here is to use the Intersect method to select the matching values in the 2 string arrays.Then use Count() to check if there's a match with the same count as orderCostCenter.


var orderCostCenter = orderDetails.Select(c => c.CostCenterId);
var listProcess = process.Where(c => c.companyID == 1 &&
  c.CostCenter.Split(',').Intersect(orderCostCenter).Count() == orderCostCenter.Count());
   
v2
Comments
Maciej Los 19-May-20 4:49am
   
5ed!
George Swan 19-May-20 7:25am
   
Thanks,Maciej. the site seems a bit slow updating today.
Quote:
i need this output using linq :
{Id=1, Name="Process 01", CostCenter="1,2,3,4" , companyID = 1 },
{Id=3, Name="Process 03", CostCenter="1,2,5" , companyID = 1 },


If CostCenterId is ordered list, you can try something like this:
void Main()
{

	List<Process > process= new List<Process >()
	{
		new Process{Id=1, Name="Process 01", CostCenter="1,2,3,4" , companyID = 1  },
		new Process{Id=2, Name="Process 02", CostCenter="1,4,10"  , companyID = 1  },
		new Process{Id=3, Name="Process 03", CostCenter="1,2,5"   , companyID = 1  },
	};
	
	List<orderDetails> orderDetails= new List<orderDetails>()
	{
		new orderDetails{ Id=1, Name="order 01", CostCenterId ="1" , companyID = 1},
		new orderDetails { Id=2, Name="order 02", CostCenterId="2" , companyID = 1},
	};

	var result = process
		.Where(x=> x.CostCenter.Contains(string.Join(",", orderDetails.Select(o=> o.CostCenterId))))
		.ToList();
	foreach(var r in result)
		Console.WriteLine($"Id={r.Id}\tName='{r.Name}'\tCostCenter='{r.CostCenter}'\tcompanyID={r.companyID}");
}

// Define other methods and classes here
public class Process
{
	public int Id;
	public string Name;
	public string CostCenter;
	public int companyID;
}

public class orderDetails
{
	public int Id;
	public string Name;
	public string CostCenterId;
	public int companyID;
}


Result:
Id=1  Name='Process 01'  CostCenter='1,2,3,4'  companyID=1
Id=3  Name='Process 03'  CostCenter='1,2,5'  companyID=1
   
Hello ,

You can use Intersect and SequenceEqual for compare the two arrays and get the output which you want.

var finalList = new List();
            process.ForEach(y =>
            {
                var vardata = orderDetails.Select(x => x.CostCenterId).ToArray();
                var data1 = y.CostCenter.Split(',').Intersect(vardata);
                if (data1.ToArray().SequenceEqual(vardata))
                {
                    finalList.Add(y);
                }                
            });
   
v2

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