2

I have looked at similar question - ids not string - but it wont work for me so I am reposting.

Store model:

public class Store
{
    public int ID { get; set; }

    public virtual IList<Asset> Assets { get; set; }
}

Till:

public class Till
{
    public int ID { get; set; }

    [ForeignKey("Store")]
    public int StoreID { get; set; }
    public virtual Store Store { get; set; }

    public int Type { get; set; }
    public string Kind { get; set; }
    public string Name { get; set; }
}

Using MvcCheckBoxList reference library I have a FilterViewModel containing list of names:

public class FilterViewModel
{
    public IEnumerable<TillCheckBox> AvailableTills { get; set; }
    public IEnumerable<TillCheckBox> SelectedTills { get; set; }
    public PostedTills PostedTills { get; set; }
}

public class PostedTills
{
    public string[] TillNames { get; set; }
}

The FilterViewModel.AvailableTills is initialised with the names of all assets of type Till (eg. { "Front", "Back", "Floor", "Shop", "Cafe" }):

private List<string> GetAllTills()
{
    return (from a in _db.Assets where (a.Kind == "Till") select a.Name).Distinct().ToList();
}

The FilterViewModel.SelectedTills and FilterViewModel.PostedTills are both empty. Nothing selected yet.

When the View uses FilterViewModel and when submits back via GET a list of selected checkboxes back to the controller in FilterViewModel .

What I am trying to do is get list of stores which have assets with name in PostedTills list.

So if user selects "Front" and "Cafe" I want stores which has assets named "Front" AND "Cafe".

Instead what I am getting is a list of stores with assets named "Front" (but not necessarily "Cafe") and assets named "Cafe" (but not "Front").

The Linq query I am trying (based on other post) is:

stores = (from s in db.Stores
join a in db.Assets on s.ID equals a.StoreID
where (a => postedTillNames.Contains(a.Name))
select s).Distinct().ToList();

But this gives an error "Cannot convert lambda expression to type 'bool' because it is not a delegate type" on:

(a => postedTillNames.Contains(a.Name))

I have also tried different queries but cannot get the results I am after.

Anyone able to help please?

Regards Craig

1 Answer 1

4

You just need to put in the actual expression that you want executed, not a lambda that defines how to do it. Replace (a => postedTillNames.Contains(a.Name)) with postedTillNames.Contains(a.Name).

The fact that you're providing the code in the context of a query expression means that the compiler will be wrapping the code up in a lambda for you. It's being wrapping it in a lambda twice (once by you, and once by the compiler), which is what's causing the problem.

Sign up to request clarification or add additional context in comments.

5 Comments

But it doesn't do a search which looks for assets with postedTillNames[0] AND postedTillNames[1], but rather returns a list with one or the other.
@CraigRoberts Currently the code will return all items that have an asset in that whose name is in that collection of names. Is that not what you want?
As above if I select "Front" and "Cafe" I want a list of Stores which have BOTH asset names "Front" AND "Cafe". At the moment it returns stores which have only one or the other, not both.
@CraigRoberts Again, this query will return all items where the asset's name is in the list provided. It's not returning items where the asset's name is equal to a single value. It sounds like you have another problem unrelated to what you have described here.
Ok, thanks. Will acknowledge your answer as correct in itself. Regs

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.