比较两个带有linq和lambda的列表,其中一个是string,另一个是long
本文关键字:另一个 long string 一个 lambda 两个 linq 比较 列表 | 更新日期: 2023-09-27 18:16:02
我在这里读了很多,但还没有找到正确比较两个列表的答案,因为一个列表是字符串列表,另一个是长列表。对于熟悉linq的人来说应该很容易。
列表1:
var fileContactIds = new List<string> {
"5678765", "2135123", "12341234", "341234123", "12341234123",
"2341234123", "341234123", "123412341", "13342354","12342341",
"123412322", "163341234", "2345234115", "8967896", "75626234"
};
2: 列表从JsonConvert.DeserializeObject<RelatedContacts>(json)?.list
返回
{
"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"
}
]
}
]
}
]
}
和映射到:
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; }
}
}}
我查询: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.
}
);
使用.Contains
作为ids列表:
.Where(item => fileContactIds.Contains(item.ClientId))
加在一起(稍微重构一下):
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));