LINQ Equal Operator Error in Query

Skitzafreak

I am trying to use LINQ in a VB.Net program to query a database. This is my query statement:

Dim query = From p In db.Parts, s In db.Suppliers
            Join sName In db.Suppliers On s.Supplier_ID Equals p.Supplier_ID
            Select p.Part_Number, p.Part_Description, sName

The error I am getting is:

You must reference at least one range variable on both sides of the 'Equals' operator.
Range variable(s) 'p', 's' must appear on one side of the 'Equals' operator, and range variable(s) 'sName' must appear on the other.

What I am trying to do, is I have a Parts Table with a Column that has a Foreign Key referencing the Supplier_ID column in my Suppliers Table. I am trying to make it so that I am grabbing the Supplier_Name field in my Supplier Table to display, instead of just the Supplier ID.

Everything I have tried looking up on Google shows me join syntax that is the same as mine, but obviously I am misunderstanding how this actually works. So what is my problem?

I've tried changing things around because of the Error Message, but nothing seems to work, either the error message remains, or I just get more errors.

Josh Part

According to microsoft docs (here and here), you have a couple issues here:

First, you are specifying both tables in the From clause, where you only have to specify the "left" part of your join.

Dim query = From p In db.Parts

Second, in the Join clause you are specifying an alias for Suppliers (sName) that you're not using.

Join s In db.Suppliers

Your final query should look like this:

Dim query = From p In db.Parts
        Join s In db.Suppliers On s.Supplier_ID Equals p.Supplier_ID
        Select p.Part_Number, p.Part_Description, sName

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してくださいdebugcn@gmail.com

編集
0

コメントを追加

0

関連記事