views:

1019

answers:

5
+1  A: 

How come you are using a comma-delimited list? The better approach would be to have a table in between which stores the mapping between users and modules.

casperOne
I agree with you but i chose this database model to be able to store the order of the modules for every user , for example user 1 have modules in this order 3,2,4
ahmed
@ahmed: You can still store order, you just have another column in the mapping table with the order and populate that in ascending order.
casperOne
+1  A: 

If you can, it might be best to refactor your tables rather than trying to deal with the data in the format it's in.

In your "usersModule" table, if you change the modules column to "moduleId" and just insert a record for every module associated with the userId, you'll have a much easier time querying the data I believe, and your database will be much easier to maintain.

Table: "modules"
moduleId    moduleName
1           Recommended...
2           Blah
3           ...

Table: "usersModule"
userId      moduleId
1           3
1           2
1           4
Andy White
I agree with but i chose this database model to be able to store the order of the modules for every user , for example user 1 have modules in this order 3,2,4
ahmed
You could add another column to the table like "order" where you could store the rank of the module for that user. You really shouldn't put a comma-separated list of IDs in a column, it makes it very difficult to query.
Andy White
I agree that that separating the columns will much easy to query but it is just one query, finally it settled out
ahmed
A: 

If it's an option, you might want to refactor your database schema. Your usersModule table should have one user id and one module id per row, and the moduleId column should be a foreign key to the module table. If you do it this way, the linq query would be trivial.

If you're stuck with the database as-is, you're probably stuck using two queries: one query to retrieve the module id's, and another query to retrieve the actual module info.

davogones
A: 

It would be something like this I think, but you haven't given enough exact information about your database structure for me to write the query exactly.

var q =
    from m in db.modules
    select new {
     m.moduleId,
     m.name,
     db.usersModules.Exists(um => um.userId == myUserId)
    };

Edit: oh I just realised you've got a comma-separated thing. Rearrange your database properly first, like the others have said.

Ray Hidayat
ive got this error "usersModules does not have difinition for Exists and extension method"?
ahmed
Well I'm not going to write your code for you! I just said it could be done as something like that.
Ray Hidayat
thanks man for trying to help :), best regards
ahmed
+2  A: 

Your query doesn't works because LinqToSQL tries to translate everything to plain SQL.

If you cannot refactor your schema, and use an intermediate table since you clearly have a many-to-many relation ship, you can do something like this:

var userModules = db.userModules.SingleOrDefault(m => m.userId == myUserId).modules;
// Get the modules of the user (a comma delimited string)
var integerModules = modules.Split(',').Select(m => int.Parse(m));
// Convert the comma delimited string to IEnumerable<int>

var query = db.modules.Select(x => new {
                x.moduleId,
                x.moduleName,
                isChecked = integerModules.Contains(x.moduleId)
}); // And finally do the query
CMS
thanks man, it works!, I have tried for long time and finally thanks :)
ahmed
Nice CMS :) Ahmed, u should refactor your DB schema. Having 2,3,4 as the data is bad. Use either a one to many table dude ... it's the proper thing to do here IMO.
Pure.Krome
Thanks Pure.Krome, I also agree with you, ahmed, you should use a linking table between users and modules, and if you want to keep track of the order (userId, moduleId, number), you can add a integer field to do that, the queries will be easier...
CMS