0

I have a list of lists that I created with LINQ to contain a status code object:

List<List<StatusLookup>> statusList = new List<List<StatusLookup>>();

foreach(var action in DbmsReport.Actions){
    statusList.Add((from stat in MyFactory.CreateQueryable<StatusLookup>()
                    where stat.ActionTypes.Contains(action)
                    select stat).ToList());
}

I now need to somehow parse this statusList and find all of the StatusLookup objects that are in each list of lists, so for example:

DbmsReport.Actions = ( sprint, fight );

StatusLookup = ( { id = 0, text = "exhausted", ActionTypes = ( sprint, fight ) }
                 { id = 1, text = "tired", ActionTypes = ( jog, sprint, fight ) }
                 { id = 2, text = "rested", ActionTypes = ( sleep ) }
               );

So in this case I'd need to get a list that contains StatusLookup for "exhausted" and "tired" in no particular order, what is the best way to get this list? As can be seen in this example, there can be either 1 or more than 1 StatusLookup for each DbmsReport

3
  • 3
    Do you have to keep them as lists-of-lists? what if you flattened it out to one big list? Easy query then Commented Sep 5, 2012 at 14:52
  • No I do not have to keep it as a list of lists, please provide an example of this flattened out list Commented Sep 5, 2012 at 14:55
  • If DbmsReport.Actions = (spring,fight) do you want all StatusLookup where ActionTypes contain sprint and fight or where ActionTypes contains sprint OR fight Commented Sep 5, 2012 at 15:02

2 Answers 2

1

Can you not just do?

var lookups= MyFactory.CreateQueryable<StatusLookup>()
             .Where(x=> DbmsReport.Actions.All(y=>x.ActionTypes.Contains(y))).ToList();

or if you want an or condition (ie Report= sprint,fight and you want to find all Lookups that have either sprint or fight) then

var lookups= MyFactory.CreateQueryable<StatusLookup>()
             .Where(x=> DbmsReport.Actions.Any(y=>x.ActionTypes.Contains(y))).ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect, the first example is what I was looking for. Thanks.
The problem is when I try the above I get a "Specified method is not supported." NotSupportedException This might have something to do with MyFactory being an Nhibernate factory...
So this did end up working, I just had to separate the MyFactory.CreateQueryable<StatusLookup>() to its own variable first, ie var allStatuses = MyFactory.CreateQueryable<StatusLookup>().ToList() then replace the MyFactory.Crea..... in our example with allStatuses
1

Try this to flatten it out, additionally you can add where clauses as necessary.

var r = from p in statusList
        from q in p
        select q;

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.