How to get data from List Using another List in C# LINQ [Resolved]

Posted by Kasani007 under LINQ on 5/2/2016 | Points: 10 | Views : 587 | Status : [Member] | Replies : 4
How to get data from List Using another List in C#



public List<Addresses> GetAddresses(List<long> Ids)
{
List<Addresses> addr=new List<Addresses>

///
return addr;

}




Responses

Posted by: A2H on: 5/2/2016 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can use the below code to extract values from address list

//Extracting the values based on id values present in ids list
 var addressList = addr.Where(item => ids.Contains(item.Id)).Select(a => a).ToList();


Complete code
 List<Addresses> addr = new List<Addresses>();
//Loading dummy values
Addresses objAddress = new Addresses();
objAddress.Id = 1;
objAddress.Address = "Test Address1";
addr.Add(objAddress);

Addresses objAddress2 = new Addresses();
objAddress2.Id = 2;
objAddress2.Address = "Test Address2";
addr.Add(objAddress2);

Addresses objAddress3 = new Addresses();
objAddress3.Id = 3;
objAddress3.Address = "Test Address3";
addr.Add(objAddress3);

//loading dummy id values
List<long> ids = new List<long>();
ids.Add(1);
ids.Add(2);

//Extracting the values based on id values present in ids list
var addressList = addr.Where(item => ids.Contains(item.Id)).Select(a => a).ToList();
foreach (Addresses obj in addressList)
{
//Loop through results and use the values

}


Add the below class

 public class Addresses
{
public long Id { get; set; }
public string Address { get; set; }
}



Sample Demo : https://dotnetfiddle.net/DxWm3l

Thanks,
A2H
My Blog

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 5/3/2016 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can try with the below code
  //Extracting the values based on id values present in ids list
var addressList = addr.Where(item => (item !=null) && (ids.Contains(item.Id))).Select(a => a).ToList();


Update code
 List<Addresses> addr = new List<Addresses>();
//Loading dummy values
Addresses objAddress = new Addresses();
objAddress.Id = null;
objAddress.Address = "Test Address1";
addr.Add(objAddress);

Addresses objAddress2 = new Addresses();
objAddress2.Id = 2;
objAddress2.Address = "Test Address2";
addr.Add(objAddress2);

Addresses objAddress3 = new Addresses();
objAddress3.Id = 3;
objAddress3.Address = "Test Address3";
addr.Add(objAddress3);

//loading dummy id values
List<long?> ids = new List<long?>();
ids.Add(1);
ids.Add(2);

//Extracting the values based on id values present in ids list
var addressList = addr.Where(item => (item !=null) && (ids.Contains(item.Id))).Select(a => a).ToList();
foreach (Addresses obj in addressList)
{
//Loop through results and use the values

}


Class:
  public class Addresses
{
public long? Id { get; set; }
public string Address { get; set; }
}


Thanks,
A2H
My Blog

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kasani007 on: 5/3/2016 [Member] Starter | Points: 25

Up
0
Down
ThanQ A2H,

i have another question that

if(item.Id) is null then it is showing object reference is not set to instance exception...

how can i use item.Id!=null in
var addressList = addr.Where(item => ids.Contains(item.Id)).Select(a => a).ToList();

Thanks In Advance....


Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 5/3/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Kasani007, you can use join

--
Thanks & Regards,
RNA Team

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response