1

I have this lambda expression:

this.SitesStore.DataSource = 
dc.Sites.Where(s => multipleSites.Select(ms => ms.Siteid )
.Contains(s.Siteid) && s.Cid == int.Parse(Session["Cid"].ToString())).ToList();

But I want to modify it with this Select new instead of Select(ms => ms.Siteid):

Select new { s.Cid, s.Siteid, FullName = dc.fn_GetSiteid(int.Parse(Session["Cid"].ToString()), s.Siteid) + " - " + s.FullName, s.Title, s.Address, s.Phone }

if I just add this Select new I get this error:

'System.Linq.IQueryable' does not contain a definition for 'Contains' and the best extension method overload 'Ext.Net.Utilities.StringUtils.Contains(string, params string[])' has some invalid arguments

multipleSites:

var multipleSites = (from cs in dc.CUsersSites
                        join c in dc.CUsers on cs.UserId equals c.UserId
                        where cs.Cid == int.Parse(Session["Cid"].ToString()) && c.UserName == HttpContext.Current.User.Identity.Name
                        select cs).ToList();

Schemas

CUsersSites (id, UserId, Cid, Siteid)
CUsers (id, UserId, UserName)
Sites (id, Cid, Siteid, FullName)
5
  • 3
    And what didn't work? Commented Mar 1, 2018 at 11:16
  • Possible duplicate of Creating a list of objects from LINQ select new Commented Mar 1, 2018 at 11:17
  • 4
    Please show the exact code that didn't work. You can't just replace a method call with Select new { ... } but you can use Select(ms => new { ... }) Commented Mar 1, 2018 at 11:17
  • What you want ot acheive with that select? The Contains method should be used on and array, but your new Select(s => new { ... }) returns an anonymous type objects array. Commented Mar 1, 2018 at 11:36
  • See the edit to my answer, does that work? Making some assumptions here... Commented Mar 1, 2018 at 14:29

1 Answer 1

3

You should just need to add the Select with the correct syntax as @JonSkeet already mentioned. Furthermore, you can simplify and avoid the issue you are getting (mentioned in your edit) by using Any(ms => ...) instead inside of the Where.

Here is the full example.

this.SitesStore.DataSource = dc.Sites
    .Where(s => multipleSites.Any(ms => ms.Siteid == s.Siteid && s.Cid == int.Parse(Session["Cid"].ToString())))
    .Select(s => new { ... })
    .ToList();

So this should compile but some things to note:

  • This assumes the type of Cid is int.
  • It seems odd to me that you would need to call ToString() presuming the Session[] IS an array of strings. Conversely, if Session holds int already then there would be no need for int.Parse(...).
Sign up to request clarification or add additional context in comments.

3 Comments

Code dumps are not considered answers, please explain your code in the answer
The op wants to use that select instead of Select(ms => ms.Siteid)
Do they? The Select you mention is nested in the Where (predicate) and should be able to remain untouched.

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.