1

I have a structure like

class a
{
 public IList<b> bs{ get; set; }
        public class b
        {
            public string r{ get; set; }
            public IList<sl> sls{ get; set; }
            public class sl
            {
                public string sn{ get; set; }
                public string st{ get; set; }
            }
        }
}

the query is like if sn == "abc" then get r I have done

 a aobj = new a();
 var aa = aobj.bs.Where(c => c.sl != null).Select(c => c).ToList(); // here I get `r = "qwerty", sls will have data like sn = "qwerty0", st= "1" ; sn = "asdf" , st="2"; sn = "zxc" st = "abc"; sn="me" , st = "abc"
 var bb = aa.where(c => c.sl.Select(dr => dr.st.ToLower().Contains("abc"))); // I 'm here checking that `sn` contain abc or not
 var cc = bb.Select(c => c.r).ToList(); // result

my expected output of query is "zxc", "me"

but I am getting all the list not only contains abc.. can anyone suggest me what should I do? I am partitioning this query to debug. Thank you

2
  • 1
    First, fix your code above to compile. Second, add some data and expected results. Then it will be easier to help you out. Commented Sep 2, 2017 at 8:41
  • I think you can use a.bs.SelectMany(c => c.sls).Where(c => c.st.Contains("abc")).Select(c=> c.sn) ;). Commented Sep 2, 2017 at 9:05

2 Answers 2

1

You'll need to use the Any operator to check if an enumerable collection has an item that meets a criteria.

You can't use Select as that only projects an item, it isn't returning an predicate and as such has no function in a where clause.

Here is your (fixed for syntax errors) changed code:

var aa = aobj.bs.Where(c => c.sls != null).Select(c => c).ToList();
// use Any here
var bb = aa.Where(c => c.sls.Any(dr => dr.sn.ToLower().Contains("abc")));
var cc = bb.Select(c => c.r).ToList();

And here is the test set I used:

a aobj = new a();
aobj.bs = new List<b>();
aobj.bs.Add(new b {
  r ="bar",
  sls = new List<sl>{
    new sl { sn="tets"},
    new sl { sn="no"}
  }
});

aobj.bs.Add(new b {
  r ="foo",
  sls = new List<sl>{
    new sl { sn="no"},
    new sl { sn="abc"}
  }
});

aobj.bs.Add(new b {
  r ="fubar",
  sls = new List<sl>{
    new sl { sn="no"},
    new sl { sn="abc"}
  }
});

This will output:

foo
fubar

If you combine all operators together you'll get:

var merged = aobj
   .bs
   .Where(c => c.sls != null 
      &&  c.sls.Any(dr => dr.sn.ToLower().Contains("abc")))
   .Select(c => c.r);
Sign up to request clarification or add additional context in comments.

3 Comments

ah! Thank you sooo much.. can you tell me why can't I use select instead of any..basically select will select data that contain abc..m i corret?
can I put where condition in any like .any(dr=>dr.name.where(c=>c.fn.contains("aa")) ?
No, you can't @Dhara because you will then check for characters in name.
0

I think you can use a code like this:

var cc = a.bs
    .Where(w => w.sls?.Any(s => s.st?.ToLower().Contains("abc") ?? false) ?? false)
    .Select(c => c.r);

Comments

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.