用linq和lambda比较两个列表,其中一个是字符串,另一个是长整数 [英] Compare two lists with linq and lambda where one is string and other long

查看:126
本文介绍了用linq和lambda比较两个列表,其中一个是字符串,另一个是长整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里已经读了很多书,但是在查询的最后一部分中找不到正确比较两个列表的答案,因为一个列表是字符串列表,另一个是长列表.对于熟悉linq的人来说应该很容易.

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.

列表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用于ID列表:

.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));

这篇关于用linq和lambda比较两个列表,其中一个是字符串,另一个是长整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆