TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Satish Bhat
NA
1.2k
5.2k
Need help in querying many to many relationship.
Jun 24 2011 3:18 AM
Hi,
Need help in querying many to many relationship.
I have 3 tables
1] Games
GameID
Game Title
1
FIFA 10
2
Halo 3
3
DIRT
2] Platforms
PlatformID
PlatformName
1
XBox 360
2
PS3
3
PSP
4
Wii
3] GamePlatforms
GameID
PlatformID
1
1
1
2
1
3
2
2
2
3
Now i need to query All Games in PlatformID
X
POCO
public class Game
{
[Key]
public int GameId { get; set; }
[Column("GameTitle")]
public string Title { get; set; }
public virtual ICollection<Platform> Platforms { get; set; }
}
public class Platform
{
[Key]
public int PlatformId { get; set; }
[Column("Platform")]
public string Name { get; set; }
public virtual ICollection<Game> Games { get; set; }
}
// Mapping
modelBuilder.Entity<Game>()
.HasMany(u => u.Platforms)
.WithMany()
.Map(a =>
{
a.ToTable("GamePlatforms");
a.MapLeftKey("GameId");
a.MapRightKey("PlatformId");
});
Service Layer
public
IEnumerable
<MemberGame> Search(int platformId, string gameName)
{
Platform _platform = platformRepository.Get(platformId);
var predicate = PredicateBuilder.True<MemberGame>();
predicate = predicate.And(mg => mg.Game.Platforms.Contains(_platform)).And(mg => mg.Game.Title == gameName).Or(mg => mg.Game.Title.Contains(gameName));
return repository.GetMany(predicate);
}
Data Layer
public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
{
return _dbSet.Where(where).ToList();
}
throws an error Unable to create a constant value of type 'XXXX.BO.Platform'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
Please help me to solve the problem.
Thanks inadvance
Satish
Reply
Answers (
1
)
How to insert a row in a gridview to affect even the table in the SQL table?
Showing signin after signout when we click on back button