How about this one:
User user = _session.QueryOver<User>().Where(x => x.securedusername == _encryptionProvider("Hamza")).SingleOrDefault();
Edited: Your query has issues as i have mentioned in the comment above. One option is, (This can be done only if the usernames are unique, otherwise there can be clash) 1. you have to come up with a index which is unique to each string value. for EX:
SAM = xyz(110111101) SAMI = xyzk(110111101001)
this index has to be inserted for each customer in the database.
then you can go for like:
`User user = _session.QueryOver<User>().Where(x => x.Index.Contains(IndexGenerator("Hamza"))).SingleOrDefault();//I do not know weather this Contains method exist in nhibernate`. But there should be. so find that :)
But this query will give some unnecessary values as well, But that can be further filtered using the actual username after decryption in the coding. That can get the precise search results.
You can use this
IndexGenerator as your
_encryptionProvider if it is cryptic.
I have never used NHibernate, but the problem is that the definition of Username is in the model and not in the database.
Basicly you have two choices (bear in mind that I've never actually used NHibernate but other entity frameworks/ORMs), and possibly a third depending on NHibernate's implementation of IQueryable.
Load the entire table into memory (if it is small and you do this query often that might be useful, because I guess NHibernate has some smart caching?):
User user = _session.QueryOver<User>().ToList().FirstOrDefault(x => x.Username == "Hamza");
The second is checking against the encrypted string, as provided by @Diode:
// Resolve string, since we are using LINQ2SQSL in some form var encName = _encryptionProvider.Encrypt("Hamza"); User user = _session.QueryOver<User>().FirstOrDefault(x => x.SecuredUsername == encName);
If you can use a specific instance of IQueryable for wrapping purposes, that would be one solution. Have a look at http://msdn.microsoft.com/en-us/library/bb351562.aspx for a description of IQueryable.
But basicly, the encryption has to happen on the server, ie not in the database, and that kinda limits your options.
EDIT: Search for all users matching "Hamz*", we have to load to memory and check there:
var users = _session.QueryOver<User>().ToList().Where(x => x.Username.StartsWith("Hamz"));
You can use a custom type to encrypt your Username (instead of doing the encryption in the User class, do the encryption in the custom type) see http://nhforge.org/blogs/nhibernate/archive/2009/02/22/encrypting-password-or-other-strings-in-nhibernate.aspx
When you query, you will only be able to query for exact matches, but you will be able to do a query like:
User user = _session.QueryOver<User>() .Where(x => x.Username == "Hamza") .SingleOrDefault();
If you want to match case-insensitively, you can convert the value to upper/lowercase in the custom type.
If you want to do LIKE searches, then you will need to look at some other type of indexing - eg. Lucene.NET and NHibernate.Search
- Query on encrypted values using LINQ
- Getting a collection of index values using a LINQ query
- How can I write the following code more elegantly using LINQ query syntax?
- ASP.NET : select multiple values in query using Linq
- Linq sub query when using a repository pattern with EF code first
- Using LINQ query result for data source for GridControl c#
- how to get two column values in a single query using linq to entities
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to return values from a LINQ query and display them in a table using C#, ASP.NET MVC and Entity Framework
- Filter linq query results using values from a list
- Using Max in LINQ query - null values in the list
- How to query a Datatable using LINQ and add values to a List?
- Query with Nullable Values using LINQ
- Store values into object of object through SQL Query using linq to entity
- how to get latest distinct 10 records without null values using linq query
- Using a Linq query to select objects where any value of a property matches values from a list
- Excluding NULL values in a datatable query using Linq
- How to query values associated with the foreign key using linq and display in listview
- How to dynamically group a list and select values using linq query in C#?
- C# Using a linq query to filter on Values in an Array
- Get Key values from a file by using linq query
- Query values from a C# Generic Dictionary using LINQ
- Compare input double with datatable string column values within a range using between query(SQL query to Linq on datatable c# )
- LINQ Intersect Query for Many to Many relationship using .NET4 MVC4 Code First
- Using linq to select possible values for multiple database columns in one SQL query (EF6)
- C# query basic BSON knowing only key and values using LINQ
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- Linq Query using GroupBy() and Sum() and Returning Values not Grouped
- Min Max values using a linq query and select box
- Query a parent-children entities with one-many relationships and retrieve children records based on values in the parent record using LINQ to Entities
- LINQ query decimal and return decimal converted to string
- How to Update Two Tables using LINQ in ASP.NET MVC 4 Web Applications
- Conditional LINQ query on XML file
- LINQ - Add static text to each result
- Querying for object based on property of inside object
- Visual C#, Find records in DataTable A which is or not in DataTable B
- Generate list of string from list of objects
- EF Loading Related Entities in Extension Methods
- Using Ardalis.Specification to query CosmosDb using Linq syntax
- How to PLINQ an existing LINQ query with Joins?
- create hyperlink in gridview asp.net c#
- Can a LINQ Expression defined as a lambda expression include other LINQ Expressions?
- Parsing a single-statement boolean expression tree
- MVC4 pass lambda from controller to repository
- How to efficiently retrieve a list of random elements from an IQueryable?
- Searching database with Linq
- Order by bigger of two Sql columns
- If user searches for "OConnors", how to get both "Niall O'Connors" & "Niall OConnors"?
- Summing a column gives null in C#/ASP.NET
- SQL Statement into LinQ Statement