How to use Linq/Group/Join to create new data from existing data

  • Thread starter Thread starter zydjohn
  • Start date Start date
Z

zydjohn

Guest
Hello:

I have data with different structure, like the following:



public class Shoes
{
public int SizeID { get; set; }
public string ProductDate { get; set; }
public string Full_Half { get; set; }
public string Style { get; set; }
}

public class Socks
{
public long SockID {get; set; }
public int SizeID { get; set; }
public string ProductDate { get; set; }
public string Full_Half { get; set; }
public decimal Price
}

public class FootWare
{
public long SockID {get; set; }
public int SizeID { get; set; }
public string ProductDate { get; set; }
public string Full_Half { get; set; }
public string Style { get; set; }
public decimal Price
}

public class Triple
{
public int SizeID { get; set; }
public string ProductDate { get; set; }
public string Full_Half { get; set; }
}

Shoes shoe1 = new Shoes { SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Full", Style = "Winter" };
Shoes shoe2 = new Shoes { SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Half", Style = "Summer" };

Socks sock1 = new Socks { SockID = 10001, SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Full", Price = 2.0m };
Socks sock2 = new Socks { SockID = 10002, SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Full", Price = 2.0m };
Socks sock3 = new Socks { SockID = 10003, SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Half", Price = 1.0m };
Socks sock4 = new Socks { SockID = 10004, SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Half", Price = 1.0m };
Socks sock5 = new Socks { SockID = 10005, SizeID = 7, ProductDate = "2019-11-28", Full_Half = "Full", Price = 2.0m };

I want to use Linq in C# to generate the following results:
FootWare foot1 = new FootWare { SockID = 10001, SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Full", Style = "Winter", Price = 2.0m }
FootWare foot2 = new FootWare { SockID = 10002, SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Full", Style = "Winter", Price = 2.0m }
FootWare foot3 = new FootWare { SockID = 10003, SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Half", Style = "Summer", Price = 1.0m }
FootWare foot4 = new FootWare { SockID = 10004, SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Half", Style = "Summer", Price = 1.0m }



The rule is, for the same Tuple structure of Triple, combine the shoes and socks together to make a new data:

For example:

For shoe1 = new Shoes { SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Full", Style = "Winter" };

Its Triple value is:

{ SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Full" }

Then check the corresponding socks, the following matching socks are found:

Socks sock1 = new Socks { SockID = 10001, SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Full", Price = 2.0m };

Socks sock2 = new Socks { SockID = 10002, SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Full", Price = 2.0m };

Then combine the both data structure to make a new data structure: Footware, build the results:

FootWare foot1 = new FootWare { SockID = 10001, SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Full", Style = "Winter", Price = 2.0m }

FootWare foot2 = new FootWare { SockID = 10002, SizeID = 6, ProductDate = "2019-11-28", Full_Half = "Full", Style = "Winter", Price = 2.0m }

If there is no corresponding same Triple value, then discard the data, for example:

Socks sock5 = new Socks { SockID = 10005, SizeID = 7, ProductDate = "2019-11-28", Full_Half = "Full", Price = 2.0m };

As its Triple value is:

{ SizeID = 7, ProductDate = "2019-11-28", Full_Half = "Full" }

Since there is no SizeID = 7 in any shoes values, then discard sock5 from the result.

I can do this with complicated loop in C#, but I want to know how to do this using Linq with others like Join, Group, etc.

Please advice.

PS: I am using VS2019 Version 16.3.10 on Windows 10 (Version 1909).

Continue reading...
 
Back
Top