asp.net - complex query using LINQ and C# -


I have a big problem solving this problem. I have 2 tables

The first table is

There is a module (ID and name)

another table

containing the user ID and their favorite modules' number

< P> Now, for example, I want to generate a list of checkboxes for IID

For example, in which the following module will be "3,2 and 4" result

Note that the "Recommended for you" checkbox is not checked because the module id "1" is not in user 1

I have a long time to solve this problem using C # and LINQ

See the result of my efforts :) (The following code does not work)

from db.usersModules. = & Gt; Um2.userId == myUserId). Single (). Module.trim () Split (',') where um.Contains (m.moduleId.ToString ()) New {module = Here I want the module id from the module table - Modulation = Here I want module from the module table Ichecked = Here I want to Depending on "true" or "false", whether the user has this module or not};

If LINQ is easy for you, please try to solve it? Any questions?

Your query does not work because LinqToSQL tries to translate everything into plain SQL

If you can not apply your schema again, and use an intermediate table because you have obviously many-to-many relationship ships, then you can do something like this. Are:

  var userModules = Db.userModules.SingleOrDefault (m => m.userId == myUserId). Module; // Get the user's modules (a comma-delimited string) var integerModules = modules.Split (','). Select (m = & gt; int.Parse (m)); // IEnumerable & lt; Int & gt; Change the comma-delimited string for Var query = db.modules.Select (x = & gt; new {x.moduleId, x.moduleName, isChecked = integerModules.Contains (x.moduleId)}); // and finally query  

Comments