i have 3 tables
Table: A
- aID
- Text
Table: B
- bID
- Text
Table: A_B (reference table which holds both of the primary keys as foreign keys)
- aID
- bID
The Entity Framework knows that Table A_B is just a reference table and does not create it in the DBModel but keeps the references in Table A and B.
My question is how do i get the data which is in the reference table without actually having this table? When i try to access the values of Table B using the reference which is in Table A i cant get the values of the Table B Colums.
ObjectQuery<A> aTable = dbConnection.A;
var result = from data in aTable
where data.aID = '12'
select data.B; //B is the reference to table B out of table A
What i need is something like:
ObjectQuery<A> aTable = dbConnection.A;
var result = from data in aTable
where data.aID = '12'
select data.B.bID;
How do i get data out of Table B using the reference between Table A and Table B?
Using: VS 2010 Prof, .Net 4, Linq2Entity
As Gary Said, you either use First (if you want one B for each A), FirstOrDefault (if you want nulls where there is no B for A) or SelectMany (if you want all Bs for each A)
The latter can be written like this:
var result = from data in dbConnection.A
from b in data.B
select b.Id;
Related
I have a query which uses the view a as follows and the query is extremely slow.
select *
from a
where a.id = 1 and a.name = 'Ann';
The view a is made up another four views b,c,d,e.
select b.id, c.name, c.age, e.town
from b,c,d,e
where c.name = b.name AND c.id = d.id AND d.name = e.name;
I have created an index on the table of c named c_test and I need to use it when executing the first query.
Is this possible?
Are you really using this deprecated 1980s join syntax? You shouldn't. Use proper explicit joins (INNER JOIN in your case).
You are joining the two tables C and D on their IDs. That should mean they are 1:1 related. If not, "ID" is a misnomer, because an ID is supposed to identify a row.
Now let's look at the access route: You have the ID from table B and the name from tables B and C. We can tell from the column name that b.id is unique and Oracle guarantees this with a unique index, if the database is set up properly.
This means the DBMS will look for the B row with ID 1, find it instantly in the index, find the row instantly in the table, see the name and see whether it matches 'Ann'.
The only thing that can be slow hence is joining C, D, and E. Joining on unique IDs is extremely fast. Joining on (non-unigue?) names is only fast, if you provide indexes on the names. I'd recommend the following indexes accordingly:
create index idx_c on c (name);
create index idx_e on e (name);
To get this faster still, use covering indexes instead:
create index idx_b on b (id, name);
create index idx_c on c (name, id, age);
create index idx_d on d (id, name);
create index idx_e on e (name, town);
I have set of tables in my data base like table A which has set of set of categories , table B set of repositeries. A and B are related by categoryid. And then table C which has set of properties for a repoId. Table C and A are associated with repoId.
Table C can have multiple values for a repoId.
The data in C table is like a property say a number string like 12345XXXX (max data of 10 characters) and I have to find the top 6 matching characters of a particular value in table C and the count of repoIds associated with those top 6 value for a particular data in table A (categoryid).
Table A(set of categories ) ---------> Table B (set of repositories, associated with A with categoryid)---------> Table V (set of FMProperties against a repoId)
Now currently, this has been achieved by using joins and substring queries on these tables and it is very slow.
I have to achieve this functionality using Elastic search. I dont have clear view how to start?
Do I create separate documents / indexes for table A , B and C or fetch the info using sql query and create a single document.
And how we can apply this analytics part explained above.
I am very new and amateur in this technology but I am following the tutorials provided at elasticsearch site.
PFB the query in mysql for this logic:-
select 'fms' as fmstype, C.fmscode as fmsCode,
count(C.repoId) as countOffms from tableC C, tableB B
where B.repoId = C.repoId and B.categoryid = 175
group by C.fmscode
order by countOffms desc
limit 1)
UNION ALL
(select 'fms6' as fmstype, t1.fmscode, t2.countOffms from tableC t1
inner join
(
select substring(C.fmscode,1,6) as first6,
count(C.repoId) as countOffms from tableC C, tableB B
where B.repoId = C.repoId and B.categoryid = 175 and length(C.fmscode) = 6
group by substring(C.fmscode,1,6) order by countOffms desc
limit 1 ) t2
ON
substring(t1.fmscode,1,6) = t2.first6 and length(t1.fmscode) = 6
group by t1.fmscode
order by count(t1.fmscode) desc
limit 1)
I have below query in which i need to assign one table column value to another table column.
Query:
SELECT A.aval,B.bval,B.bval1 FROM A JOIN B ON (A.aval = B.bval)
How do I assign one table column value to another table column in Hive?
Have tried
SELECT A.aval,B.bval,B.bval1, A.aval = B.bval1 FROM A JOIN B ON (A.aval = B.bval)
In results:
A.aval = B.bval1, returning false since its not assigning to A.aval.
I guess you want to write in a table ?
So You have to create a table (for example C) which contains all the fields you need.
And then you do :
INSERT [OVERWRITE] INTO TABLE C
SELECT A.aval,B.bval,B.bval1, A.aval
FROM A
JOIN B ON (A.aval = B.bval)
The result of the select will be inserted in the table C
insert overwrite table c SELECT A.aval,B.bval,B.bval1 FROM A JOIN B ON (A.aval = B.bval)
How to join two table from two different edmx using linq query..
Is there a way to query from 2 different edmx at a time.
Thanks.
Update
As per your comment, EF wasn't able to parse a combined Expression tree across 2 different contexts.
If the total number of records in the tables is relatively small, or if you can reduce the number of records in the join to a small number of rows (say < 100 each), then you can materialize the data (e.g. .ToList() / .ToArray() / .AsEnumerable()) from both tables and use the Linq join as per below.
e.g. where yesterday is a DateTime selecting just a small set of data from both databases required for the join:
var reducedDataFromTable1 = context1.Table1
.Where(data => data.DateChanged > yesterday)
.ToList();
var reducedDataFromTable2 = context2.Table2
.Where(data => data.DateChanged > yesterday)
.ToList();
var joinedData = reducedDataFromTable1
.Join(reducedDataFromTable2,
t1 => t1.Id, // Join Key on table 1
t2 => t2.T1Id, // Join Key on table 2
(table1, table2) => ... // Projection
);
However, if the data required from both databases for the join is larger than could reasonably expected to be done in memory, then you'll need to investigate alternatives, such as:
Can you do the cross database join in the database? If so, look at using a Sql projection such as a view to do the join, which you can then use in your edmx.
Otherwise, you are going to need to do the join by manually iterating the 2 enumerables, something like chunking - this isn't exactly trivial. Sorting the data in both tables by the same order will help.
Original Answer
I believe you are looking for the Linq JOIN extension method
You can join any 2 IEnumerables as follows:
var joinedData = context1.Table1
.Join(context2.Table2,
t1 => t1.Id, // Join Key on table 1
t2 => t2.T1Id, // Join Key on table 2
(table1, table2) => ... // Projection
);
Where:
Join Key on table 1 e.g. the Primary Key of Table 1 or common natural
key
Join Key on table 2, e.g. a Foreign Key or common natural key
Projection : You can whatever you want from table1 and table2, e.g.
into a new anonymous class, such as new {Name = table1.Name, Data = table2.SalesQuantity}
I have a database which consists of the following:
** Table 1 **
Id (PK)
Field1
** Table 2 **
Id (PK)
Field2
** Link Table **
Table1Id (FK)
Table2Id (FK)
The problem is, I cannot access Table 2 from Table 1, even though the relationship exists in the database.
For example, the following should be possible:
var Results = from c in DataContext.Table1
where c.Table2.Field2 == "Test"
select c;
However, "c.Table2.Field2" is not available for some reason - all I get for "c.Table2." is the following (among the standard any<>, where<> et al):
RelationshipName
RelationshipSet
SourceRoleName
TargetRoleName
So obviously something is screwy somewhere, but I cannot work out what!
Both tables exist in the Entity Schema, and have a valid relationship between them.
The reason that c.Table2.Field2 is not available is that c.Table2 doesn't have a property called Field2. c.Table2 is a list of entities which have that property, not an instance of that entity. It's not clear what your intention is here, but I think you want:
var Results = from c in DataContext.Table1
where c.Table2.Any(t2 => t2.Field2 == "Test")
select c;