l0lander
l0lander

Reputation: 2153

Linq to Entity Framework m2m query with simple string constraint

Trying to learn Linq to EF in MVC3 project...
Many to many relationship in Entity Framework:

        Shirt              ShirtType           Type
     -----------        -------------        ---------
       ShirtID             ShirtID            TypeID
      ShirtName            TypeID            TypeName
      ShirtPrice

No other fields in ShirtType maping table so now I've got navigation properties in both entities Shirt.Type and Type.Shirt.
Before generating entities from database i've created complex PK in ShirtType including both foreign keys.
I've populated the database with values.
Here's the code which i used to query the database in order to return shirts that are referenced with type Type.Name==tName

public ActionResult Browse(string tName)
        {
            using (TShopEntities db=new TShopEntities())
            {
                var typeModel = from s in db.Shirt
                                from t in s.Type
                                where t.TypeName == tName
                                select new tsStore.ModelView.ShirtView()
                                {
                                    ShirtName=s.ShirtName,
                                    ShirtPrice=s.ShirtPrice
                                };
                return View(typeModel.ToList());
            }
        }

My linq query returns nothing and the view prints just headers. I'm stuck here for a while now, is the linq query wrong or should i look for some other error?

Upvotes: 0

Views: 718

Answers (2)

RPM1984
RPM1984

Reputation: 73112

Assuming this is the query you want:

return shirts that are referenced with type Type.Name==tName

And assuming your EF model and database is correct (hard to tell, since i can't see a screenshot of your EDMX or your FK's).

You should have 2 entities on your EDMX - Shirt and Type. The join table will be hidden as EF is smart enough to do an implicit join behind the scenes.

Then this query should work:

var shirtsOfAParticularType = db.Shirts.Where(shirt => shirt.Types.Any(type => type.TypeName == "someTypeName")).ToList();

Remember, since it's a many..many, a shirt can have many types. So the query above will return shirts that have at least one type of the one you specify.

Is that what you want?

Upvotes: 1

Naor
Naor

Reputation: 24063

When you load all the three entities to the edmx, the entity framework created two entities and not three:

A. Shirt - with scalar properties of ShirtID, ShirtName and ShirtPrice.   
           with navigation property to many Types (IEnumerable<Type>).   
B. Type - with scalar properties of TypeID and TypeName.   
          with navigation property to many Shirts (IEnumerable<Shirt>).  

Then, after creating the code for your entities, you can do:

public ActionResult Browse(string tName)
        {
            using (TShopEntities db=new TShopEntities())
            {
                var typeModel = from s in db.Type
                                select Shirts;
                return View(typeModel.ToList());
            }
        }

And thats all. Don't consist creating another entity for ShirtType because this is not the right way of creating your model.

Upvotes: 0

Related Questions