2

please help in creating simple c# Linq query

List<A> X;

class A{
public string phone;
...some other props
}
class B{
public string xyz;
public List<B> bobj;
}
class C{
public string pqr;
public B;
}
..

Now I have list of C;

List<C> tobesearched;

How to get all C i.e. List<C> from tobesearched for a phone which contains "123"

4
  • Thanks, Sorry I couldn't post it properly Commented Dec 24, 2019 at 23:13
  • 2
    I believe bobj should be of type List<A>. But anyway, what did you try so far? Can you show your query and describe results you got with that query Commented Dec 24, 2019 at 23:13
  • Frankly, no clue.. cad.SelectMany(s => s.Customer.phonenumbers) .FirstOrDefault(s => s.phone == kp.Value)) Commented Dec 24, 2019 at 23:17
  • your tobesearched list does not include any A not sure how would you like to search for any phone in it. Commented Dec 24, 2019 at 23:26

1 Answer 1

1

If I understood your question correctly, you're looking to filter your List<C> where any B has a bobj which has a phone containing 123, right?

IEnumerable<C> result = tobesearched.Where(t => t.b.bobj.Any(u => u.phone.Contains("123")));

..or with the class you posted in the comment below:

IEnumerable<Cdaily_snapshot> result = 
        tobesearched.Where(t => t.customer.phonenumbers.Any(u => u.phone.Contains("123")));
Sign up to request clarification or add additional context in comments.

4 Comments

yes correct, but your solution didn't work. List<C> result = tobesearched.Where(t => t.b.bobj.Any(u => u.phone.Contains("123"))); it returns List of B not C
Here is my actual classes class Cdaily_snapshot{ public Customer customer;} class Customer{List<CphoneNumbers> phonenumbers;} class CphoneNumbers{public bool isDefault; public string phone;} Now in another class I have List<Cdaily_snapshot> cad; need to find all List<Cdaily_snapshot> from cad where phone is like 123 Sorry I dont know how to put it properly. Evertime I press enter, it is getting posted.
I edited my answer. This expression does indeed return an IEnumerable<Cdaily_snapshot>.
Sorry man it worked. Just added ToList<C>(). Thanks for help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.