Reputation: 63
I have two lists,
private List<DealResponse> L1 = new List<DealResponse>
{
new DealResponse {detailId = "5", detailcd = "ABC", fileName = "string 1", isNgo = "0"},
new DealResponse {detailId = "5", detailcd = "DEF", fileName = "string 2", isNgo = "0"},
new DealResponse {detailId = "5", detailcd = "XYZ", fileName = "string ", isNgo = "0"}
};
private List<DealResponse> L2 = new List<DealResponse>
{
new DealResponse {detailId = "5", detailcd = "ABC", fileName = "string 11", isNgo = "1"},
new DealResponse {detailId = "6", detailcd = "MNO", fileName = "string 3", isNgo = "1"}
};
I'm trying to write a method which accepts detailId and return the result by merging above two list, and if there is duplicate (duplicate definition = when detailId, detailcd match between two lists) select entry from L2
so after merging result would be
var Result = new List<DealResponse>
{
new DealResponse {detailId = "5", detailcd = "ABC", fileName = "string 11", isNgo = "1"},
new DealResponse {detailId = "5", detailcd = "DEF", fileName = "string 2", isNgo = "0"},
new DealResponse {detailId = "5", detailcd = "XYZ", fileName = "string ", isNgo = "0"},
new DealResponse {detailId = "6", detailcd = "MNO", fileName = "string 3", isNgo = "1"},
};
Note that in result we selected this entry from L2 since detailId = 5, detailcd = ABC was duplicate
public List<DealResponse> GetDealResponse(string detailId)
{
// My main confusion is while doing union how to handle the case
// which I mentioned (On duplicate select entry from second list)
var L3 = L1.Union(L2, new DealResponseComprarer()).ToList();
}
public class DealResponse
{
public string detailId { get; set; }
public string detailcd { get; set; }
public string fileName { get; set; }
public string isNgo { get; set; }
}
public class DealResponseComprarer : IEqualityComparer<DealResponse>
{
public bool Equals(DealResponse x, DealResponse y)
{
return x.detailId == y.detailId && x.detailcd == y.detailcd ;
}
public int GetHashCode(DealResponse obj)
{
return (obj.detailId.GetHashCode() + obj.detailcd.GetHashCode());
}
}
Upvotes: 1
Views: 93
Reputation: 32248
In case you actually want to filter your results using detailId
, since this value is passed to the GetDealResponse()
method, you could add a .Where
condition to the equalized Union
list.
public class DealResponse
{
public string detailId { get; set; }
public string detailcd { get; set; }
public string fileName { get; set; }
public string isNgo { get; set; }
}
public List<DealResponse> GetDealResponse(string detailId)
{
return L2.Union(L1, new DealResponseComprarer())
.Where(elm => elm.detailId.Equals(detailId)).ToList();
}
L1 = new List<DealResponse>() {
new DealResponse() { detailId = "5", detailcd = "ABC" , fileName = "string 1", isNgo = "0" },
new DealResponse() { detailId = "5", detailcd = "DEF" , fileName = "string 2", isNgo = "0" },
new DealResponse() { detailId = "5", detailcd = "XYZ" , fileName = "string ", isNgo = "0" }};
L2 = new List<DealResponse>() {
new DealResponse() { detailId = "5", detailcd = "ABC" , fileName = "string 11", isNgo = "1" },
new DealResponse() { detailId = "6", detailcd = "MNO" , fileName = "string 3", isNgo = "1" }};
string ID = "5";
List<DealResponse> L3 = GetDealResponse(ID);
Which would return this list:
{ detailId = "5", detailcd = "ABC" , fileName = "string 11", isNgo = "1" }
{ detailId = "5", detailcd = "DEF" , fileName = "string 2", isNgo = "0" }
{ detailId = "5", detailcd = "XYZ" , fileName = "string ", isNgo = "0" }
Upvotes: 2
Reputation: 52260
I'm trying to write a method which accepts detailId and return the result by merging above two list, and if there is duplicate (duplicate definition = when detailId, detailcd match between two lists) select entry from L2
Another way to say this is:
This can be accomplished with one line:
var combined = L2.Concat(L1.Except(L2, new DealResponseComprarer()));
Upvotes: 1