Left Outer Join with IEnumerable - entity-framework-4

using Entity Framework 4.1 is it possible to perform a left outer join with an IEnumerable?
For example, lets say i want to count the number of orders for each month.
var range = Enumerable.Range(1,6).AsQueryable();
var result = range
.GroupJoin(context.Orders, i => i, o => o.Month, new
{
Month = i,
Count = m.Count()
});
Which would produce the following
Month, Count
1, 0
2, 0
3, 10
4, 20
5, 0
6, 0

Only if you call context.Orders.ToList() and load all orders to your application. IEnumerable is in your application memory and context.Orders represent data in your database. You cannot join data in database with data in memory of your application. Converting IEnumerable by AsQueryable will not put it to database.
To do your requirement directly with ADO.NET and SQL you must either create and fill temporary table and perform a database join or create stored procedure with table valued parameter to perform database join. Neither technique is supported by EF.

Related

Ignore ID automatic order in Find

I am trying to retrieve a collection of data by an order like this:
#all_data= Data.find(#data_ids)
#data_ids - have the ids of the data to be retrieved. For instance:
Data
ID Name
1 A
2 B
3 C
4 D
If the #data_ids are like [3,2,4] I want to retrieve the data in that order, sou it would be C, B, D... The thing is it always retrieve the data in B, C, D order. Is that possible to do? To ignore that order and to retrieve it by the given params order?
The thing is,
I have two tables, table A and table Data.
Table A:
Relation, Data_ID, ORDER
1; 1; 2;
1; 2; 1;
2; 3; 3;
So what I want to do is to retrieve data_id by order from the relation one I will have (2,1), and I am doing it, but when I find (2,1) I receive 1,2.
In MySQL there is a function that gives you the power to override custom ordering - take a look at "Sort by specific ids in ActiveRecord".
If you are using Postgres, you may consider this approach (credit to
Omar Qureshi for his answer in "ActiveRecord.find(array_of_ids), preserving order":
unsorted = Model.find(arr)
sorted = arr.inject([]){|res, val| res << unsorted.detect {|u| u.id == val}}
Just fetch records from database, and sort them in memory. This works when what you wanna fetch is relatively a small amount.
Data.find(#data_ids).sort_by!{|data| #data_ids.index(data.id)}
P.S. the reason why you always get the data in id's order is that primary keys are always indexed, and the database traverses indexes in some order (asc or desc).

Entity Framework returns zero/null values for columns after 1st column

I am using EF6.1.2, database-first.
In one situation, this linq to sql query:
var data = from dl in ctx.ObjectContext.DocumentLines.Where(drow => drow.DocumentId == documentId)
join di in ctx.ObjectContext.DocumentsInfoes on dl.DocumentId equals di.Id
join ba in ctx.ObjectContext.Addresses on dl.DeliveryAddressId equals ba.Id into tba
from xba in tba.DefaultIfEmpty()
join sc in ctx.ObjectContext.SalesCategories on dl.SalesCategoryId equals sc.Id into tsc
from xsc in tsc.DefaultIfEmpty()
select new { Line = dl, DocumentInfo = di, DeliveryAddress = xba, SalesCategory = xsc };
var abc = data.ToList();
is not working.
The first documentline is correctly bound, but subsequent rows have some NULL or zero properties. If I run the generated SQL query, all columns are present and correct.
Is this a bug in Entity Framework? I have downloaded the EF source and I want to step into it to attempt to understand what's happening but don't know where to start looking.
The documentline table has a timestamp Version column for concurrency purposes. I have read a few similar issues where problems with a timestamp column, or other columns, (see Entity Framework does not pull through data from some columns) could cause subsequent columns to fail.
EDIT
On further investigation, it seems that, in the one call to this code that exhibits the problem, I can step over var abc = data.ToList(); without hitting any of the breakpoints in the setters of the properties of the DocumentLine entity, in the data model. Bizarre. All other calls to the code hit the breakpoints. Why isn't it loading the entities?

How to perform update in Apache Spark SQL

I have to update a JavaSchemaRDD with some new values by having some WHERE conditions.
This is the SQL query which I want to convert into Spark SQL:
UPDATE t1
SET t1.column1 = '0', t1.column2 = 1, t1.column3 = 1
FROM TABLE1 t1
INNER JOIN TABLE2 t2 ON t1.id_column = t2.id_column
WHERE (t2.column1 = 'A') AND (t2.column2 > 0)
Yup I got solution my self. I have achieved this using Spark core only, I have not used Spark-Sql for this. I have 2 RDD's (also can be called as tables or datasets) t1 and t2. If we observe my query in the question I am updating t1 based on one join condition and two where conditions. Meaning I need three columns(id_column, column1 and column2) from t2. So I have taken these columns in to 3 individual collections. And then I put an iteration over 1st RDD t1 and during the iteration I have added those three condition statements(1 Join and 2 where conditions) using java "if" conditions. So based on "if" conditions result first RDD values got updated.

How to select the max record for each of a group of candidates in Grails?

I have a table that gets populated every day with records from reporting systems.
I have a list of the serial numbers those i am interested in returning in an asset list.
How do I get Grails to return the records that match the maximum "epoch" entry for each asset? In sql I would cross join the table back to itself after picking out the maximum such as:
select a.* from assetTable a inner join (select sn, max(epoch) epoch from assetTable group by sn) b on a.sn = b.sn and a.epoch = b.epoch
but I cannot figure out how to get this done efficiently with Grails...
From a domain class perspective it is pretty simple. Consider for the same of example that I have a single domain class "AssetTable" and it has Integer epoch, String sn, ...
Literally, all I want to do is get the latest entry (all fields) for a subset of serial numbers (sn) that I have in a List.

Can I combine these two Linq queries

I am trying to combine SharePoint data with a legacy database data and I can get the data, but I need to do it in two queries. Here are the two Linq queries:
var query =
(from dtEai in result.AsEnumerable()
join allAP in dtAllAirports.AsEnumerable()
on dtEai.Field<int>("AirportID") equals allAP.Field<int>("ID")
select new
{
Region = allAP.Field<string>("region")
}
);
and the second which gets me my result:
var join =
(
from table in query
group table by table["Region"] into groupedTable
select new
{
Key = groupedTable.Key,
Count = groupedTable.Count()
}
);
not being an expert in Linq I converted the SharePoint lists into datatables so I could do the join. Can I combine this into a single query?
I had to make two linq methods due to the fact that I did not want to try to method chain and do a groupby afterwards. It just would of been too confusing.

Resources