Afshin Ghazi Afshin Ghazi - 1 month ago 11
C# Question

Compare two lists with linq and lambda where one is string and other long

I have read alot here but haven't found the answer to correctly compare two lists in the final part of my query since one list is string list and other is a long list. should be easy for someone familiar with linq.

List 1 :

var fileContactIds = new List<string> {
"5678765", "2135123", "12341234", "341234123", "12341234123",
"2341234123", "341234123", "123412341", "13342354","12342341",
"123412322", "163341234", "2345234115", "8967896", "75626234"
};


List 2:
returned from
JsonConvert.DeserializeObject<RelatedContacts>(json)?.list


sample:

{
"type":"com.kurtosys.api.userprofile.domain.RelatedContactList",
"list":[
{
"objectlistid":5678765,
"objectlisttypeid":4567876,
"objectlistname":"ALL.National",
"clienttaxonomyid":765677,
"clienttaxonomy":"National Wholesaler",
"order":1,
"contacts":[
{
"personid":7654345678,
"fullname":"Person Jallo",
"userid":876567,
"userclientcode":"5678765",
"contactdetails":[
{
"contactid":8765567,
"contacttypeid":4565,
"contactdata":"person.contact@site.com"
},
{
"contactid":876545678,
"contacttypeid":4565,
"contactdata":"Baltimore,MD,21209,United States"
},
{
"contactid":87654567,
"contacttypeid":4584,
"contactdata":"410-413-2640"
}
]
}
]
},
{
"objectlistid":765678,
"objectlisttypeid":40400461,
"objectlistname":"RM.Internal",
"clienttaxonomyid":7567898,
"clienttaxonomy":"Internal Regional Wholesaler",
"order":2,
"contacts":[
{
"personid":56789876,
"fullname":"Jackson Man",
"userid":876567,
"userclientcode":"12341234",
"contactdetails":[
{
"contactid":309598309,
"contacttypeid":76546,
"contactdata":"mister.jackson@@site.com.com"
},
{
"contactid":876567,
"contacttypeid":4581,
"contactdata":"Baltimore,MD,21209,United States"
},
{
"contactid":876567,
"contacttypeid":2342,
"contactdata":"123-413-2604"
}
]
}
]
},
{
"objectlistid":309571364,
"objectlisttypeid":40400461,
"objectlistname":"RM.External",
"clienttaxonomyid":309580710,
"clienttaxonomy":"External Regional Wholesaler",
"order":3,
"contacts":[
{
"personid":302736188,
"fullname":"Phal Sumi",
"userid":303826019,
"userclientcode":"1016023",
"contactdetails":[
{
"contactid":309598253,
"contacttypeid":2342,
"contactdata":"misters.emailas@site.com"
},
{
"contactid":309611930,
"contacttypeid":2342,
"contactdata":"Baltimore,MD,21209,United States"
},
{
"contactid":34234132,
"contacttypeid":3422,
"contactdata":"342-803-1793"
}
]
}
]
}
]
}


and mapped to :

namespace Company.AppServices.RestClient.Users
{

public class RelatedContacts
{
public string type { get; set; }
public List<RelatedContactList> list { get; set; }
}

public class RelatedContactList
{
public long objectlistid { get; set; }
public long objectlisttypeid { get; set; }
public string objectlistname { get; set; }
public long clienttaxonomyid { get; set; }
public string clienttaxonomy { get; set; }
public int order { get; set; }
public List<PersonContact> contacts { get; set; }
}

public class PersonContact
{
public long personid { get; set; }
public string fullname { get; set; }
public long userid { get; set; }
public string userclientcode { get; set; }
public List<ContactType> contactdetails { get; set; }
}

public class ContactType
{
public long contacttypeid { get; set; }
public string contacttype { get; set; }
public long contactid { get; set; }
public long personid { get; set; }
public string contactdata { get; set; }
public int countrycode { get; set; }
public int areacode { get; set; }
public long phonenumber { get; set; }

}
}}


My query:

var res =
relatedContact.SelectMany(
rc =>
{
return rc.contacts
.Select(pc =>
{
return new
{
ClientId = pc.userclientcode,
Description = rc.clienttaxonomy,
Fullname = pc.fullname,
Email = pc.contactdetails != null && pc.contactdetails.Count >= 1 ? pc.contactdetails[0].contactdata : "",
Address = pc.contactdetails != null && pc.contactdetails.Count >= 2 ? pc.contactdetails[1].contactdata : "",
PhoneNumber = pc.contactdetails != null && pc.contactdetails.Count >= 3 ? pc.contactdetails[2].contactdata : "",
};
});
})
.Where(
el =>
{
//Stuck here -> need only items where userclientcode in second list matches an item in first list.
}
);

Answer

Use .Contains for the ids list:

.Where(item => fileContactIds.Contains(item.ClientId))

All together (with a bit of refactoring):

var res = relatedContact.SelectMany(rc => rc.contacts.Select(pc => new
            {
                ClientId = pc.userclientcode,
                Description = rc.clienttaxonomy,
                Fullname = pc.fullname,
                Email = pc.contactdetails != null && pc.contactdetails.Count >= 1 ? pc.contactdetails[0].contactdata : "",
                Address = pc.contactdetails != null && pc.contactdetails.Count >= 2 ? pc.contactdetails[1].contactdata : "",
                PhoneNumber = pc.contactdetails != null && pc.contactdetails.Count >= 3 ? pc.contactdetails[2].contactdata : "",
            }))
           .Where(el => fileContactIds.Contains(el.ClientId));