Perform a Linq Many to Many Query - c#

In SQL I have 2 tables.
Sport Athlete
SportId AthleteId
SportName AthleteName
Their relation is many to many. So there is a third table that join them.
AthleteSport
AthleteId
SprortId
If i create an entity data model using the wizard the third table is disapeared and only creates the 2 main tables with the many to many relation.
how can I perform a query to find out what kind of sports athlete 'A' does? How to change them or add a new sport?
I want to use linq and c#.
Thank you.

In your Sport entity, there will be a so called "navigation property" Athletes that contains all Athletes that belong to that Sport instance.
The same is true the other way around.

Can't you do A.Sports and get that list?

Related

Is there a way to search a list in a SQLite database?

I am creating a health and nutrition application and I am using unity as my engine. I have a SQLite database which holds the recipes, the fields are ID (primary key), Name, Ingredients and Method. Each recipe is a record in the database and they have their own lists of ingredients. The intent is to allow the user to input ingredients and the program will filter the recipe by ingredients
I expect that when the user inputs flour (as an example) it will show all the recipes that use flour. E.g.: Apple pie, cake and so on. Instead if i want to search the ingredients the input has to be identical to the entire lest
You need to read up on how relational databases work. Right now, with everything in one table, your database isn't really any more useful than a simple Excel spreadsheet.
You want three tables; one for ingredients, one for recipes, and one to join them, which I'll call Ingredient_Recipe. Ingredient_Recipe table will have two foreign keys, one from the ingredients table, one from the recipe table. Then you can do SQL queries like this example:
select Recipes.recipeName
from Recipes
INNER JOIN Ingredient_Recipe ON recipe_ID = Ingredient_Recipe.recipe_ID
Where Ingredient_ID = "oregano"
Which should tell you every recipe that contains oregano.
the classic relational way to do that is to have an ingredients table and a relator table (ingedientrecipe) so that you can query for all the ingredients in a recipe or all recipes that contain a given ingredient

Linq to SQL - cross referencing two tables in one query

I have the following scenario I need to accomplish via Linq to SQL:
I have a table called JOBS that contains these two columns:
JobID
JobName
I have another table called JOBREFS that contains these two columns:
JobID
JobRefID
I have the following query (I already have the JOBS.JobID)
Select JobRefID from JOBREFS where JOBREFS.JobID = JOBS.JobID
Then I need to do THIS query:
Select JobName from JOBS where JOBS.JobID = JOBREFS.JobRefID
I know I can do this in two queries, but thought there might be a way to get the JobName that is associated with the JobRefID in another table.
Hope this makes sense.
If the navigation properties are correct in your models, you can just use those navigation properties instead of joins. Some examples are here:
http://coding.abel.nu/2012/06/dont-use-linqs-join-navigate/

Wanting a Many to Many relationship with single cell

Now i'm sure this is an amateur question and that i'm going about this the wrong way, that's why i'm asking here! So please go easy.
I'm trying to create a database in Access 2013 that will store orders for a POS system (It isn't a commercial product, it's a computing project in C#). It contains several tables.
My issue is that I can't have multiple menuitems for every orderitem. Is there a way to do this?
Thank you in advance!
Create a MenuOrderItem table that goes between OrderItems and MenuItems. It will have a 1-to-Many relationship to both OrderItems and MenuItems. That's how Many-to-Many relationships are built in relational dbs...with an intermediary table.
In your case, to do this, remove the relationship between OrderItems and MenuItems. Create table called MenuOrderItem (or something similar). It should have just two columns in it: a column named OrderItemId, and a column named MenuItemId. These columns will be used to make the two new relationships: the first between MenuOrderItem and OrderItems and the second between MenuOrderItem and MenuItems.
One way would be to put a table between orderItems and menuItems with 2 columns. orderItemID and menuItemID. Then create your relationships. This would allow you to have one orderItem with many menuItems.

Entity Framework Many to Many columns concept

I've been used to this kind of convention before starting to work with Entity Framework/MVC:
tblMyItem (id, id_lookup1, val2, val3, val4...) - single, generic table
tblkLookup1 (id, val1, val2,...) - one to many relation table
tblmMyItemLookup2 (id, id_myitem, id_lookup2) - many to many relations table.
Recently I've found somewhere on the web that it's not good to create id column in tblmMyItemLookup2 when using Entity Framework, but I couldn't find more information on that. Could anyone please explain, why this is so significant?
When designing many-2-many relationship(i.e Student - Teacher) based on intermediate table (Student2Teacher) with only 2 foreign key columns, you'll end up having entities with navigation properties without intermediate table(no entity will be created for that table in context at all):
Student.Teachers <- EntityCollection<Teacher>
Teacher.Students <- EntityCollection<Student>
Meanwhile, if you add any extra field to your intermediate table, you'll have navigation properties pointing to intermediate entity:
Student.Student2Teacher
Teacher.Student2Teacher
making your querying uselessly more complex
In general we use lookup tables to macth associated records in two different tables that have a many to many relationship. For instance, let we have three tables: Students, Professors and StudentsProfessors. Since, a student can atten lesson that are serviced by many professors and a professor can teach more than one leasons then this is clearly a many to many relationship. So the lookup table called StudentsProfessors is used to match a student to her/his professors and vice versa. Now, the use of an id for each record of this lookup table is meaningless. We are not going to use this number anywhere. We just need to know that Student with studentId=10 is associated with professors with ids in (1,2,4,9). Just that and not the id of this record in the lookup table.

Comparing SQL tables and getting Values using Entity Framework

I have three tables viz: Person(PersonID(INT), Personname(varchar)) , Items(ItemID(INT), Itemname(varchar)) and PersonItemAssoc(PersonItemAssoc(Int), PersonID(INT), ItemID(INT)).
PersonItemAssoc is having many to many association for personid and Itemid.
I want to get way in which if I pass the itemId, I should get all the PersonIds which dont have an association witn this ItemId in the PersonItemAssoc table.
I am using Entity Framework 4.0.
Please suggest a way for implementing this.
var peopleWithoutItem = from p in Context.Person
where !p.PersonItems.Any(pi => pi.Item.ItemId == someItemId);
select p;
Note that if you get rid of PersonItemAssoc(int) and make the PersonItemAssoc PK the compound of PersonID and ItemID then the EF can do People to Items as a many to many, instead of two 1 to many relationships.

Categories