Linq To NHibernate: .StartsWith on multiple properties - linq

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?

Related

How can I concatenate two strings into one using LLBLGen Pro

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?

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 + ",")));

EntityFramework Linq query

I cannot understand what is wrong with following query:
var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp;
Should this be same as:
select * from mytable
where Barcode like '%11%' and Description like '%EW%';
If I run this in sql server i get four rows as it should be, but not when I run the linq query
i get 0 rows.
Can please someone help me. It is rather a simple query and it is giving me such a headache. Thank you very much
You forget fetch data, do this:
var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();
Also do not call AsEnumerable soon, use it as below:
var tmp = ctx.mytable.Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();
dbset.AsEnumerable<mytable>()...
Don't do that!
You are causing all your data to get pulled from the database before checking the Where condition.
Other than that, it's not really clear why your query isn't working. Your syntax is correct. I'm guessing the data doesn't actually look like you think it does. Either that, or you're expecting like %EW% to match something in a case-insensitive manner and since the Where clause is being evaluated by LINQ to Objects you're not getting that behavior.
Run a query with only one condition? " c.Barcode.Contains("11") ".
That code should run fine.

LINQ 2 Entities-query is not working, but why?

everyone! ))
Here is code.
var existingEntities = (from record in globalOne.serviceContext.records_out
where record.timestamp.Date == DateTime.Now.Date
select record ).ToList();
It doesn't work.
Another code:
var existingEntities = (from record in globalOne.serviceContext.records_out
where record.timestamp.Day == DateTime.Now.Day
select record ).ToList();
It does work.
So, problem id in next string:
where record.timestamp.**Date** == DateTime.Now.Date
also won't do
where record.timestamp.Date.Equals(DateTime.Now.Date)
But why? I have no clue. "Timestamp" field is dateTime field in MS SQL SERVER.
And - there is NO records in table.
And I almost forgot - what does it mean - "doesn't work".
App just will not reach the breakpoint after that query(first), without any error, without anything.
Thanks.
You can call record.timestamp.Date because EF can't convert it to required expression tree (then convert it to sql command). In fact EF supports limited number of functions and properties, But for DateTime, EF has some good Canonical functions. You can use them in your case, e.g you can use Day(),Month(),Year() functions to solve your problem (see the link).

how can I see the inputs to a LINQ query?

I've got a LINQ query that looks like this (at the end):
var query = from myTable0 ...
where myTable1.attributeId == 123 && (bunchaStrings.Contains(myTable1.attributeName)) && myTable2.yesNoValue == 'Y'
When I see the query it turns into this
SELECT ... FROM ... INNER JOIN ... WHERE ... AND (UNICODE([t3].[yesNoValue]) = #p3
So what's happening here is that the value of 'Y' is getting turned into '89' via the UNICODE function. That's all fine, but I'd really like to just be able to see the value of #p3 directly and I can't figure out how to see that value via any methods available from my var.
I would recommend piping the generated SQL out to the output window. There you will be able to see the whole SQL and your parameters values. Then it can also be logged.
Code for it can be found here ->
http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11
Or an easier method (if you've got a handy console around):
MyDataContext context = new MyDataContext()
context.Log = Console.Out
You may also be interested in the LINQ to SQL Visualizer : http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx

Resources