How can I concatenate two strings into one using LLBLGen Pro - linq

As the error says, I think LLBLGen have their own custom functions but official documentation doesn't help much. Is there any way to concatenate these two strings and make into one while LINQ querying?
var viewModelCarrierDivisionQuery = from division in database.CarrierDivision
where division.CarrierId == carrierId
join carrier in database.Carrier
on division.CarrierId equals carrier.CarrierId
select new CarrierDetailViewModel.DivisionModel {
Id = division.CarrierDivisionId,
Name = string.Join(" / ", carrier.Name, division.DivisionName)
};
SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryConstructionException: 'Method call to 'Join' doesn't have a known mapped database function or other known handler.'

What I have used is just a simple solution with plus name +" / "+name. This works. Is there any better way out there?

Related

check one CSV is contained in another CSV column in database LINQ to Entities

I have a table called Products having column called ApprovedState which contains values like "AL,AK,AR". I'm getting a CSV list from front-end as listCSVState='AL,AK'.
Can someone help me to write a LINQ query which will return me all products which are approved in listCSVState. I have tried following code but not getting the correct result.
from product in db.Products where ((listCSVState== null) || (listCSVState.Contains(product.StateApprovals)))
Trivial in LINQ to Objects, tricky in LINQ to Entities due to lack of string.Split support.
Still, a combination of Any, string.Contains and string concatenations can do the job, like this
var states = listCSVState.Split(',');
var query = db.Products.Where(product => states.Any(state =>
("," + product.StateApprovals + ",").Contains("," + state + ",")));

Joining two tables and returning multiple records as one row using LINQ

I'm trying to write a LINQ expression that will join two tables and return data in a format similar to what is possible using MySql's GROUP_CONCAT. I tried searching around on Google and SO, but all the results I found used MSSQL or were only using one table. The expression I have written now looks like this:
from d in division
join o in office on d.Id = o.DivisionId
select new
{
id = d.Id,
cell = new string[] { d.DivisionName, o.OfficeName }
}
As expected, this returns a list of every division and what offices correspond to that division. The only problem is that since most divisions will have more than one office, I get a division back for each office in said division. Essentially I'm seeing results like this:
Division1: Office1
Division1: Office2
Division1: Office3
Division2: Office1
When I want to see:
Division1: Office1, Office2, Office3
Division2: Office1
I remember doing something a while ago with MySql that used GROUP_CONCAT, but I can't figure out what the equivalent of that would be using LINQ. I tried writing a method which had an IEnumerable<Office> parameter and built a string using the Aggregate extension method, but the way I have my LINQ expression written now, each Office is passed in rather than an IEnumerable<Office>. Is there a better way to approach this problem than what I'm doing now? I'm rather new to LINQ expressions, so I apologize if this is trivial.
You want a group join, e.g.
from d in division
join o in office on d.Id = o.DivisionId into offices
select new
{
id = d.Id,
divisionName = d.DivisionName,
officeNames = offices.Select(o => o.OfficeName)
}

CRM LINQ + Creating Dynamic where clause for anonymous types

var crm = new XrmDataContext("Crm");
var properties = from property in crm.awx_propertyawx_properties
orderby property.awx_name
select new {
awx_name = property.awx_name == null ? "no name" : property.awx_name
}
;
properties = properties.Where(a => a.awx_name.StartsWith("Sears Tower"));
I get the error "Cannot determine the attribute name" - what am I doing wrong here? I read in plenty of threads that this is perfectly okay to do. HELP!
I too spent quite a while trying to find a way of issuing dynamic where clauses against the CRM system.
I tried similar syntax above and also built a predicate builder. Both did not work.
In the end I had to take a 2 stage approach.
1. Pull a superset from CRM using static where clause into a collection
2. Query dynamically from my in memory collection using standard techniques.
I hate CRM.
Phil

Entity Framework - "Unable to create a constant value of type 'Closure type'..." error

Why do I get the error:
Unable to create a constant value of type 'Closure type'. Only
primitive types (for instance Int32, String and Guid) are supported in
this context.
When I try to enumerate the following Linq query?
IEnumerable<string> searchList = GetSearchList();
using (HREntities entities = new HREntities())
{
var myList = from person in entities.vSearchPeople
where upperSearchList.All( (person.FirstName + person.LastName) .Contains).ToList();
}
Update:
If I try the following just to try to isolate the problem, I get the same error:
where upperSearchList.All(arg => arg == arg)
So it looks like the problem is with the All method, right? Any suggestions?
It looks like you're trying to do the equivalent of a "WHERE...IN" condition. Check out How to write 'WHERE IN' style queries using LINQ to Entities for an example of how to do that type of query with LINQ to Entities.
Also, I think the error message is particularly unhelpful in this case because .Contains is not followed by parentheses, which causes the compiler to recognize the whole predicate as a lambda expression.
I've spent the last 6 months battling this limitation with EF 3.5 and while I'm not the smartest person in the world, I'm pretty sure I have something useful to offer on this topic.
The SQL generated by growing a 50 mile high tree of "OR style" expressions will result in a poor query execution plan. I'm dealing with a few million rows and the impact is substantial.
There is a little hack I found to do a SQL 'in' that helps if you are just looking for a bunch of entities by id:
private IEnumerable<Entity1> getByIds(IEnumerable<int> ids)
{
string idList = string.Join(",", ids.ToList().ConvertAll<string>(id => id.ToString()).ToArray());
return dbContext.Entity1.Where("it.pkIDColumn IN {" + idList + "}");
}
where pkIDColumn is your primary key id column name of your Entity1 table.
BUT KEEP READING!
This is fine, but it requires that I already have the ids of what I need to find. Sometimes I just want my expressions to reach into other relations and what I do have is criteria for those connected relations.
If I had more time I would try to represent this visually, but I don't so just study this sentence a moment: Consider a schema with a Person, GovernmentId, and GovernmentIdType tables. Andrew Tappert (Person) has two id cards (GovernmentId), one from Oregon (GovernmentIdType) and one from Washington (GovernmentIdType).
Now generate an edmx from it.
Now imagine you want to find all the people having a certain ID value, say 1234567.
This can be accomplished with a single database hit with this:
dbContext context = new dbContext();
string idValue = "1234567";
Expression<Func<Person,bool>> expr =
person => person.GovernmentID.Any(gid => gid.gi_value.Contains(idValue));
IEnumerable<Person> people = context.Person.AsQueryable().Where(expr);
Do you see the subquery here? The generated sql will use 'joins' instead of sub-queries, but the effect is the same. These days SQL server optimizes subqueries into joins under the covers anyway, but anyway...
The key to this working is the .Any inside the expression.
I have found the cause of the error (I am using Framework 4.5). The problem is, that EF a complex type, that is passed in the "Contains"-parameter, can not translate into an SQL query. EF can use in a SQL query only simple types such as int, string...
this.GetAll().Where(p => !assignedFunctions.Contains(p))
GetAll provides a list of objects with a complex type (for example: "Function"). So therefore, I would try here to receive an instance of this complex type in my SQL query, which naturally can not work!
If I can extract from my list, parameters which are suited to my search, I can use:
var idList = assignedFunctions.Select(f => f.FunctionId);
this.GetAll().Where(p => !idList.Contains(p.FunktionId))
Now EF no longer has the complex type "Function" to work, but eg with a simple type (long). And that works fine!
I got this error message when my array object used in the .All function is null
After I initialized the array object, (upperSearchList in your case), the error is gone
The error message was misleading in this case
where upperSearchList.All(arg => person.someproperty.StartsWith(arg)))

Linq To NHibernate: .StartsWith on multiple properties

I'm trying to accomplish the following query (notice .StartsWith):
return (from p in _session.Linq<Profile>()
where (p.Firstname + " " + p.Lastname).StartsWith(wildcard)
select p).ToList();
This throws: could not resolve property: Firstname.Lastname.
If I do this:
return (from p in _session.Linq<Profile>()
where p.Firstname.StartsWith(wildcard)
select p).ToList();
Everything is working. How can this be?
Thanks in advance!
The Where Expression does not know how to handle the concatenation of strings. It is trying to make sense of the properties, not the values.
Also, for future reference the StartsWith with the concat and the other one with out would in practice return the same thing.
Is this what you want?
return (from p in _session.Linq<Profile>()
where p.Firstname.StartsWith(wildcard) || p.Lastname.StartsWith(wildcard)
select p).ToList();
Update: rewritten answer based on new insights and edited questions.
What's in wildcard and what's the expected output vs. input? If you concat "Abel" + " " + "Braaksma" it will return true for wildcard.StartsWith("Abel") or wildcard.StartsWith("Abel Br") but not wildcard.StartsWith("Braaks"). Do you perhaps mean Contains instead? But this won't solve your error:
The exception you receive seems to come from NHibernate, not from your code. Is it possible that Lastname does not have a correct mapping to the database table? Can you show the stacktrace? Can you access the properties outside of the context of the LINQ statement, but filled with data from the table?

Resources