Merging two Query Results - asp.net-mvc

I have following two Queries on Same Table and need to merge it.
var Prod = this.UnitOfWork.myRepository.GetData();
var PreProd = this.UnitOfWork.myRepository.GetData();
var Merge = Prod.Union(PreProd);
But when i check results of Merge it doesn't show Union but shows following message
Message: This method supports the LINQ to Query Result Union not
working Entities infrastructure and is not intended to be used
directly from your code
How this can be accomplished.

You can use .AsEnumerable() to convert IQueryable to IEnumerable firstly.
var Prod = this.UnitOfWork.myRepository.GetData().AsEnumerable();
var PreProd = this.UnitOfWork.myRepository.GetData().AsEnumerable();
var Merge = Prod.Union(PreProd);

LINQ to EF supported Union:
IQueryable<TSource> Union<TSource>(
this IQueryable<TSource> source1,
IEnumerable<TSource> source2
)
so try:
var Prod = this.UnitOfWork.myRepository.GetData();
var PreProd = this.UnitOfWork.myRepository.GetData();
var Merge = Prod.Union(PreProd.AsEnumerable());

Related

dapper querymultiple spliton error

I'm using: ASP.NET MVC, MySql, Dapper.NET micro-orm
I made a stored procedure with 3 SELECTs, two of which returns lists and the third one returns an integer.
Here is my code:
using (var conn = new MySqlConnection(GetConnectionString()))
{
var readDb = conn.QueryMultiple(storedProcedure, parameters, commandType: CommandType.StoredProcedure);
var result = new someView
{
TopicsList = readDb.Read<ITopic>().ToList(),
TopTopicsList = readDb.Read<IMessage>().ToList(),
TopicsCount = readDb.Read<int>().Single()
};
return result;
}
In ITopic I have TopicId, in IMessage I have MessageId.
And here's the error:
When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id Parameter name: splitOn
I tried adding splitOn on both QueryMultiple and Read, and nigher accepted it.
Though I dont understand why I need splitOn? can't dapper see that I have three separate SELECTs? When using conn.Read(storedProcedure,parameters) on each of the selects separately (instead of MultipleQuery on all of the together) dapper has no problem mapping it to a given object.
What am I doing wrong?
1) Problem solved when I used the real models names instead of their interfaces names:
TopicView instead of ITopic, TopTopicsView instead of IMessage;
2) Once that was fixed and there was no longer "no splitOn" error, started another problem with the < int > casting in line:
TopicsCount = readDb.Read<int>().Single()
probably mysql doesnt return numbers back as ints?
I tried using decimal, object, dynamic, etc.. with no luck. Eventually fixed it by creating another Model with int property inside that has the same name as the database int parameter and now it works.
3) Here's the final working code:
using (var conn = new MySqlConnection(GetConnectionString()))
{
var parameters = context.MapEntity(query);
var multi = conn.QueryMultiple(storedProcedure, parameters, commandType: System.Data.CommandType.StoredProcedure);
var TopicsList = multi.Read<TopicView>().ToList();
var TopTopicsList = multi.Read<TopTopicsView>().ToList();
var result = multi.Read<HomeView>().Single();
result.TopicsList = TopicsList;
result.TopTopicsList = TopTopicsList;
return result;
}

breeze.js mixing AND and OR predicates

I'm trying to configure the proper syntax for breeze.js when mixing AND and OR predicates. I did not see an example on the breeze site and could not find one anywhere else.
Basically I want to do something like this in my WHERE clause:
(
age > 30 AND
sex == 'M' AND
jobStartDate >= '1/1/2000'
)
OR
(
exemptStatus == 1
)
this will bring back entities that match the 3 criteria OR are exempt. I'm using EF6 with ODATA syntax.
Thanks
Try this:
var Predicate = breeze.Predicate;
var baseQuery = EntityQuery.from("Something");
var pred1 = new Predicate("age", ">", 30);
var pred2 = new Predicate("sex", "==", 'M');
var pred3 = new Predicate("jobStartDate", ">=", new Date(2000,1,1));
var pred4 = new Predicate("exemptStatus", "==", 1);
var compositePred = pred1.and(pred2).and(pred3).or(pred4);
var query = baseQuery.where(compositePredicate);
myEntityManager.executeQuery(query);
or
var compositePred = Predicate.and(pred1, pred2, pred3).or(pred4);

Search a collection of objects

I'm trying to search for a collection of potential nodes but unable to do it...
I have a product that has a relationship with many instances. I would like to query the DB and get all the instances that are in a list that i get from the user.
Cypher:
var query = _context
.Cypher
.Start(new
{
instance = startBitsList,
product = productNode.Reference,
})
.Match("(product)-[:HasInstanceRel]->(instance)")
.Return(instance => instance.Node<ProductInstance>());
The problem is startBitsList... I use StringBuilder to generate a query that contains all the instances I'm looking for:
private static string CreateStartBits(IEnumerable<string> instanceNames)
{
var sb = new StringBuilder();
sb.AppendFormat("node:'entity_Name_Index'(");
foreach (var id in productIds)
{
sb.AppendFormat("Name={0} OR ", id);
}
sb.Remove(sb.Length - 4, 4);
sb.Append(")");
var startBitsList = sb.ToString();
return startBitsList;
}
I get exceptions when trying to run this cypher...
Is there a better way to search for multiple items that are stored in the collection I get from the user?
OK, I think there are a couple of issues at play here, first I'm presuming you are using Neo4j 1.9 and not 2.0 - hence using the .Start.
Have you tried taking your query and running it in Neo4j? This should be your first port of call, typically it's easy to add a breakpoint on the .Results call and add a 'watch' for query.Query.DebugText.
However, I don't think you need to use the StartBits the way you are, I think you'd be better off filtering with a .Where as you already have the start point:
private static ICypherFluentQuery CreateWhereClause(ICypherFluentQuery query, ICollection<string> instanceNames)
{
query = query.Where((Instance instance) => instance.Name == instanceNames.First());
query = instanceNames.Skip(1).Aggregate(query, (current, localInstanceName) => current.OrWhere((Instance instance) => instance.Name == localInstanceName));
return query;
}
and your query becomes something like:
var prodReference = new NodeReference<Product>(2);
var query =
Client.Cypher
.ParserVersion(1, 9)
.Start(new {product = prodReference})
.Match("(product)-[:HasInstanceRel]->(instance)");
query = CreateWhereClause(query, new[] {"Inst2", "Inst1"});
var resultsQuery = query.Return(instance => instance.As<Node<Instance>>());
2 things of note
We're not using the indexes - there is no benefit to using them as you have the start point and traversing to the 'instances' is a simple process for Neo4j.
The 'CreateWhereClause' method will probably go wrong if you pass in an empty list :)
The nice thing about not using the indexes is that - because they are legacy - you are set up better for Neo4j 2.0

Breeze implementation of 'AND' and 'OR' in the where method

Breeze can query for condition1 AND condition2 by using multiple where statements. But can it query for condition1 OR condition2?
If not, what is the recommended way of achieving the same effect when using Breeze?
I am currently thinking the only way to do this is to perform the query on the server with a special action method, which creates some problems.
Use the Predicate like this sample:
var p1 = new breeze.Predicate("IsArchived", "==", false);
var p2 = breeze.Predicate("IsDone", "==", false);
var predicate = p1.and(p2);
var query = new EntityQuery("Todos").where(predicate);
Documentation is available here:
http://www.breezejs.com/documentation/query-filter
OK, the problem is the Breeze API and Docs are a little lacking with some typos.
The answer is to use the 'or' method of the Predicate class to create a new Predicate object that is passed to the where method.
Given 3 predicates you 'or' them together like so:
var newPred = p1.or(p2, p3);
or
var preds = [p2, p3];
var newPred = p1.or(preds);
or fluent method:
var p4 = Predicate.create("ShipCity", "startswith", "F")
.or("Size", "gt", 2000);
or using the static 'or' method
var newPred = Predicate.or(p1, p2, p3);

LINQ - Join Two Entities into One IQueryable Complex Type - EF4 and ASP.Net MVC

I have code like:
var entityX = this._xService.GetAll();
var entityY = this._yService.GetAll();
Both are returned as IEnumerable types. I need to inner join the two into a something like a JoinedList class that has to be IQueryable.
I'm looking for ways to do this please using LINQ.
Many Thanks
LINQ to Objects is excellent glue:
var entityX = this._xService.GetAll();
var entityY = this._yService.GetAll();
var joinedSequence = from x in entityX
join y in entityY on x.Key equals y.Key
select new { x, y };
var joinedQueryable = joinedSequence.AsQueryable();
You shouldn't really need that last step; IQueryable<T> is unnecessary because LINQ to Objects works just fine with IEnumerable<T>.

Resources