Fields with several choices and dynamic linq

c# dynamic-linq linq linq-to-sharepoint sharepoint-2010

Question

I'm making a SP2010 page where user can filter items by their phase (It's a multichoice field). For performance reasons and the nature of the filters I had to resort to System.Linq.Dynamic to make this query.

I've tried the following (this is not real code but exemplifies what I'm doing):

var lstObjects = new List<object>();
var query = "Phase = @0 ";
lstObjects.Add(Phase.Value);

context.myList.Where(query,lstObjects.ToArray());

This works if the item has only one phase and is the one being filtered. If the item has multiple phases (like Phase 1 and 2) and I filter by Phase 1, it must show. How can I filter the multichoice fields?

EDIT: Forgot to mention, the type of the object "Phase.Value" is a flag enum that was generated using SPMetal.

1
0
1/19/2011 10:31:34 AM

Popular Answer

Try context.myList.Where(m=>lstObjects.Contains(m.Phase)) or something in that context. If this isn't the solution you are looking for please provide with more information on what you mean with "Multi Phases"

a.e, does the user get to select multiple phases or does the object have mutliple phases, or both?

[edit]

I'm not real familiar with dynamic linq but I assume this could be a solution:

var lstObjects = new List<object>();
var query = "Phase == {0}";
lstObjects.Add(Phase.Value);
for (int i = 1; i < Phase.Count; i++)
{ 
    query += string.Format(" || Phase == {0}", i);
}

context.myList.Where(query,lstObjects.ToList());

or

for (int i = 0; i < Phase.Count; i++)
{ 
    if (i > 0) { query+= " || "; }
    query += string.Format("Phase == {0}", lstObjects[i].Value);
}
context.MyList.Where(query);

Let me know if this works.

[/edit]

0
1/24/2011 7:39:30 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow