I'm adding a "search" functionality to a web app I'm working on and I have the following action method:
public PartialViewResult SearchEmployees(string search_employees)
{
var employeeList = _db.Employees.ToList();
var resultList = employeeList.Where(t => t.FirstName.Contains(search_employees)).ToList();
return PartialView(resultList)
}
here I'm trying to filter out all employees that have a first name that contains the search string, however I keep getting a null list. Am I using the lambda expression wrong?
another question, is .Contains case sensitive? (I know in java theres .equals and .equalsIgnoreCase, is there something similar to this for .Contains?)
The problem here was the .ToList() in the first line.
.NET's string.Contains() method is, by default, case sensitive. However, if you use .Contains() in a LINQ-to-Entities query, Contains() will follow the case sensitivity of the database (most databases are case insensitive).
When you called .ToList() in the first line, it pulled down all of your data from the database, so the second line was doing an ordinary .NET .Contains(). Not only did it give you unexpected results, it's also terrible for performance, so please make a point to use a query before you use .ToList() (if you even use .ToList() at all).
public PartialViewResult SearchEmployees(string search_employees)
{
var employeeList = _db.Employees;
var resultList = employeeList.Where(t => t.FirstName.Contains(search_employees))
.ToList();
return PartialView(resultList)
}
Can you try the following code?
public PartialViewResult SearchEmployees(string search_employees)
{
var employeeList = _db.Employees.ToList();
var resultList = employeeList;
if(!String.IsNullOrEmpty(search_employees))
resultList = employeeList.Where(t => t.FirstName.Contains(search_employees)).ToList();
return PartialView(resultList)
}
Thanks,
Amit
Related
I'm filtering the results of a list of items in LINQ, I have seen two methods of doing it and wondered which (if any) is better. One is the method I came up with after playing around with the Intellisense, the other is from the ASP.NET MVC tutorial (found here)
My method
// GET: ProductVersions
public ActionResult Index(string productName)
{
var productVersions = db.ProductVersions.Include(p => p.LicenceVersion).Include(p => p.Product);
if (!string.IsNullOrEmpty(productName))
{
productVersions = productVersions.Where(s => s.Product.Name == productName);
}
return View(productVersions.ToList());
}
Tutorial Method
public ActionResult Index(string movieGenre)
{
var GenreLst = new List<string>();
var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.movieGenre = new SelectList(GenreLst);
var movies = from m in db.Movies
select m;
if (!string.IsNullOrEmpty(movieGenre))
{
movies = movies.Where(x => x.Genre == movieGenre);
}
return View(movies);
}
My questions
Is there a notable difference in performance, particularly as the second option is quite verbose
Is there a stylistic convention that I am missing by using my method
Is there any other possible advantage to using the second method
~Edit~
Turns out I need the ViewBag data in order to be able to populate a dropdown filter on the front end (more's the pity), so my actual code worked out as follows:
// GET: ProductVersions
public ActionResult Index(string productName)
{
//Get list of product names to filter by
var ProductLst = new List<string>();
var ProductQry = from pv in db.ProductVersions
orderby pv.Product.Name
select pv.Product.Name;
ProductLst.AddRange(ProductQry.Distinct());
ViewBag.productName = new SelectList(ProductLst);
//Populate product versions
var productVersions = db.ProductVersions.Include(p => p.LicenceVersion).Include(p => p.Product);
//Filter by product name
if (!string.IsNullOrEmpty(productName))
{
productVersions = productVersions.Where(s => s.Product.Name == productName);
}
return View(productVersions.ToList());
}
It is only the last part of the example code that is comparable to your question - the genre list is something else in the UI that isn't present in your code, and that's fine. So all we are comparing is:
var productVersions = db.ProductVersions.Include(p => p.LicenceVersion)
.Include(p => p.Product);
if (!string.IsNullOrEmpty(productName))
{
productVersions = productVersions.Where(s => s.Product.Name == productName);
}
return View(productVersions.ToList());
vs
var movies = from m in db.Movies
select m;
if (!string.IsNullOrEmpty(movieGenre))
{
movies = movies.Where(x => x.Genre == movieGenre);
}
return View(movies);
These are virtually identical - the main difference is the extra includes in your code, which is fine if you need them.
They are so comparable that there is nothing relevant to talk about in terms of comparisons.
Personally I prefer the ToList() in your example as it forces the data to materialize in the controller rather than the view. A counter to that is that having the view have a queryable allows the view to compose it further. I don't want my view composing queries, but that is a stylistic point.
The code samples you provided have quite some differences, but assuming you are asking about extension methods syntax vs query syntax, the answers I believe are as follows:
There is no difference in performance. The compiler treats the samples identically.
You are not missing any stylistic convention. I usually find chaining extension methods to be more readable and maintainable
There can be a case made for using query syntax when you want to leverage multiple range variables. Check out this answer:
LINQ - Fluent and Query Expression - Is there any benefit(s) of one over other?
I understand that you use Eager Loading. I think EagerLoading is not necessary if your model isn't so complex.
For this reason use Layz loading; Also I try to refactor your code. Look at my code style and codeLine count :) This can help for you especially huge class :) H
http://www.entityframeworktutorial.net/EntityFramework4.3/lazy-loading-with-dbcontext.aspx
At the below link you can get detailed Info.
The sample class can translate linke this also
public ActionResult Index(string productName)
{
if(string.IsNullOrEmpty(productName)) return View(new List<ProductVersions>());
return View(db.ProductVersions.Where(s => s.Product.Name == productName).ToList());
}
Lastly your comprission is wrong between "your exp and tutorial". Because ViewModel used In TutorialExp. This is usefull to prevent directly DbCon object using by Client's. In my opinion
using(var db=new DbEntities())
is required for this tutorial.
The below code will select all the rows in a table, but I want to only select the id of the row that I pass into the method. I've tried it multiple ways and now starting fresh to see if I can get it work. Any help is appreciated.
Here's my code:
[WebMethod]
public static string getProjectByID(int id)
{
using (dbPSREntities4 myEntities = new dbPSREntities4())
{
var thisProject = myEntities.tbProjects.ToList();
JavaScriptSerializer serializer = new JavaScriptSerializer();
var json = serializer.Serialize(thisProject);
return json;
}
}
You'll want to use the Where method to filter the data:
var thisProject = myEntities.tbProjects.Where(x => x.yourIdColumn == id).ToList();
The where clause is fairly simple here. You just need to use a lambda expression to specify the condition you're matching on. If you're not familiar with them more info can be found here http://msdn.microsoft.com/en-us/library/bb397687.aspx the basics are you have a variable name followed by => on the right hand side you can do what you want with the variable and I find it easiest to think of it as working like a foreach where the variable name you declare is the iteration variable (an item in the list).
var thisProject = myEntities.tbProjects.Where(x => x.Id == id).ToList();
I'm still very new to LINQ and PLINQ. I generally just use loops and List.BinarySearch in a lot of cases, but I'm trying to get out of that mindset where I can.
public class Staff
{
// ...
public bool Matches(string searchString)
{
// ...
}
}
Using "normal" LINQ - sorry, I'm unfamiliar with the terminology - I can do the following:
var matchedStaff = from s
in allStaff
where s.Matches(searchString)
select s;
But I'd like to do this in parallel:
var matchedStaff = allStaff.AsParallel().Select(s => s.Matches(searchString));
When I check the type of matchedStaff, it's a list of bools, which isn't what I want.
First of all, what am I doing wrong here, and secondly, how do I return a List<Staff> from this query?
public List<Staff> Search(string searchString)
{
return allStaff.AsParallel().Select(/* something */).AsEnumerable();
}
returns IEnumerable<type>, not List<type>.
For your first question, you should just replace Select with Where :
var matchedStaff = allStaff.AsParallel().Where(s => s.Matches(searchString));
Select is a projection operator, not a filtering one, that's why you are getting an IEnumerable<bool> corresponding to the projection of all your Staff objects from the input sequence to bools returned by your Matches method call.
I understand it can be counter intuitive for you not to use select at all as it seems you are more familiar with the "query syntax" where select keyword is mandatory which is not the case using the "lambda syntax" (or "fluent syntax" ... whatever the naming), but that's how it is ;)
Projections operators, such a Select, are taking as input an element from the sequence and transform/projects this element somehow to another type of element (here projecting to bool type). Whereas filtering operators, such as Where, are taking as input an element from the sequence and either output the element as such in the output sequence or are not outputing the element at all, based on a predicate.
As for your second question, AsEnumerable returns an IEnumerable as it's name indicates ;)
If you want to get a List<Staff> you should rather call ToList() (as it's name indicates ;)) :
return allStaff.AsParallel().Select(/* something */).ToList();
Hope this helps.
There is no need to abandon normal LINQ syntax to achieve parallelism. You can rewrite your original query:
var matchedStaff = from s in allStaff
where s.Matches(searchString)
select s;
The parallel LINQ (“PLINQ”) version would be:
var matchedStaff = from s in allStaff.AsParallel()
where s.Matches(searchString)
select s;
To understand where the bools are coming from, when you write the following:
var matchedStaff = allStaff.AsParallel().Select(s => s.Matches(searchString));
That is equivalent to the following query syntax:
var matchedStaff = from s in allStaff.AsParallel() select s.Matches(searchString);
As stated by darkey, if you want to use the C# syntax instead of the query syntax, you should use Where():
var matchedStaff = allStaff.AsParallel().Where(s => s.Matches(searchString));
I'm migrating some stuff from one mysql server to a sql server but i can't figure out how to make this code work:
using (var context = new Context())
{
...
foreach (var item in collection)
{
IQueryable<entity> pages = from p in context.pages
where p.Serial == item.Key.ToString()
select p;
foreach (var page in pages)
{
DataManager.AddPageToDocument(page, item.Value);
}
}
Console.WriteLine("Done!");
Console.Read();
}
When it enters into the second foreach (var page in pages) it throws an exception saying:
LINQ to Entities does not recognize the method 'System.String
ToString()' method, and this method cannot be translated into a store
expression.
Anyone know why this happens?
Just save the string to a temp variable and then use that in your expression:
var strItem = item.Key.ToString();
IQueryable<entity> pages = from p in context.pages
where p.Serial == strItem
select p;
The problem arises because ToString() isn't really executed, it is turned into a MethodGroup and then parsed and translated to SQL. Since there is no ToString() equivalent, the expression fails.
Note:
Make sure you also check out Alex's answer regarding the SqlFunctions helper class that was added later. In many cases it can eliminate the need for the temporary variable.
As others have answered, this breaks because .ToString fails to translate to relevant SQL on the way into the database.
However, Microsoft provides the SqlFunctions class that is a collection of methods that can be used in situations like this.
For this case, what you are looking for here is SqlFunctions.StringConvert:
from p in context.pages
where p.Serial == SqlFunctions.StringConvert((double)item.Key.Id)
select p;
Good when the solution with temporary variables is not desirable for whatever reasons.
Similar to SqlFunctions you also have the EntityFunctions (with EF6 obsoleted by DbFunctions) that provides a different set of functions that also are data source agnostic (not limited to e.g. SQL).
The problem is that you are calling ToString in a LINQ to Entities query. That means the parser is trying to convert the ToString call into its equivalent SQL (which isn't possible...hence the exception).
All you have to do is move the ToString call to a separate line:
var keyString = item.Key.ToString();
var pages = from p in context.entities
where p.Serial == keyString
select p;
Cast table to Enumerable, then you call LINQ methods with using ToString() method inside:
var example = contex.table_name.AsEnumerable()
.Select(x => new {Date = x.date.ToString("M/d/yyyy")...)
But be careful, when you calling AsEnumerable or ToList methods because you will request all data from all entity before this method. In my case above I read all table_name rows by one request.
Had a similar problem.
Solved it by calling ToList() on the entity collection and querying the list.
If the collection is small this is an option.
IQueryable<entity> pages = context.pages.ToList().Where(p=>p.serial == item.Key.ToString())
Hope this helps.
Upgrading to Entity Framework Version 6.2.0 worked for me.
I was previously on Version 6.0.0.
Hope this helps,
Change it like this and it should work:
var key = item.Key.ToString();
IQueryable<entity> pages = from p in context.pages
where p.Serial == key
select p;
The reason why the exception is not thrown in the line the LINQ query is declared but in the line of the foreach is the deferred execution feature, i.e. the LINQ query is not executed until you try to access the result. And this happens in the foreach and not earlier.
If you really want to type ToString inside your query, you could write an expression tree visitor that rewrites the call to ToString with a call to the appropriate StringConvert function:
using System.Linq;
using System.Data.Entity.SqlServer;
using System.Linq.Expressions;
using static System.Linq.Expressions.Expression;
using System;
namespace ToStringRewriting {
class ToStringRewriter : ExpressionVisitor {
static MethodInfo stringConvertMethodInfo = typeof(SqlFunctions).GetMethods()
.Single(x => x.Name == "StringConvert" && x.GetParameters()[0].ParameterType == typeof(decimal?));
protected override Expression VisitMethodCall(MethodCallExpression node) {
var method = node.Method;
if (method.Name=="ToString") {
if (node.Object.GetType() == typeof(string)) { return node.Object; }
node = Call(stringConvertMethodInfo, Convert(node.Object, typeof(decimal?));
}
return base.VisitMethodCall(node);
}
}
class Person {
string Name { get; set; }
long SocialSecurityNumber { get; set; }
}
class Program {
void Main() {
Expression<Func<Person, Boolean>> expr = x => x.ToString().Length > 1;
var rewriter = new ToStringRewriter();
var finalExpression = rewriter.Visit(expr);
var dcx = new MyDataContext();
var query = dcx.Persons.Where(finalExpression);
}
}
}
In MVC, assume you are searching record(s) based on your requirement or information.
It is working properly.
[HttpPost]
[ActionName("Index")]
public ActionResult SearchRecord(FormCollection formcollection)
{
EmployeeContext employeeContext = new EmployeeContext();
string searchby=formcollection["SearchBy"];
string value=formcollection["Value"];
if (formcollection["SearchBy"] == "Gender")
{
List<MvcApplication1.Models.Employee> emplist = employeeContext.Employees.Where(x => x.Gender == value).ToList();
return View("Index", emplist);
}
else
{
List<MvcApplication1.Models.Employee> emplist = employeeContext.Employees.Where(x => x.Name == value).ToList();
return View("Index", emplist);
}
}
I got the same error in this case:
var result = Db.SystemLog
.Where(log =>
eventTypeValues.Contains(log.EventType)
&& (
search.Contains(log.Id.ToString())
|| log.Message.Contains(search)
|| log.PayLoad.Contains(search)
|| log.Timestamp.ToString(CultureInfo.CurrentUICulture).Contains(search)
)
)
.OrderByDescending(log => log.Id)
.Select(r => r);
After spending way too much time debugging, I figured out that error appeared in the logic expression.
The first line search.Contains(log.Id.ToString()) does work fine, but the last line that deals with a DateTime object made it fail miserably:
|| log.Timestamp.ToString(CultureInfo.CurrentUICulture).Contains(search)
Remove the problematic line and problem solved.
I do not fully understand why, but it seems as ToString() is a LINQ expression for strings, but not for Entities. LINQ for Entities deals with database queries like SQL, and SQL has no notion of ToString(). As such, we can not throw ToString() into a .Where() clause.
But how then does the first line work? Instead of ToString(), SQL have CAST and CONVERT, so my best guess so far is that linq for entities uses that in some simple cases. DateTime objects are not always found to be so simple...
My problem was that I had a 'text' data type for this column (due to a migration from sqlite).
Solution: just change the data type to 'nvarchar()' and regenerate the table.
Then Linq accepts the string comparison.
I am working on retiring Telerik Open Access and replacing it with Entity Framework 4.0. I came across same issue that telerik:GridBoundColumn filtering stopped working.
I find out that its not working only on System.String DataTypes. So I found this thread and solved it by just using .List() at the end of my Linq query as follows:
var x = (from y in db.Tables
orderby y.ColumnId descending
select new
{
y.FileName,
y.FileSource,
y.FileType,
FileDepartment = "Claims"
}).ToList();
Just turn the LINQ to Entity query into a LINQ to Objects query (e.g. call ToArray) anytime you need to use a method call in your LINQ query.
I'm trying to get an object back from an NHibernate query.
My method is as follows:
public Site GetSiteByHost(string host)
{
var result = _session.CreateCriteria<Site>()
.Add(SqlExpression.Like<Site>(g => g.URLName, host));
return result;
}
the problem is, result is a type of HNibernate.ICriteria.
How can I get this to return a Site object?
If I was doing this with LINQ to SQL it'd be something like .FirstOrDefault() but that's not available with NHibernate... or is it?!?!
You need to first execute the query (by calling List<T>() on the criteria) before calling FirstOrDefault. Notice that this query might return multiple objects:
IEnumerable<Site> sites = _session
.CreateCriteria<Site>()
.Add(SqlExpression.Like<Site>(g => g.URLName, host))
.List<Site>();
And you could take the first one:
Site result = sites.FirstOrDefault();
or directly:
public Site GetSiteByHost(string host)
{
return _session
.CreateCriteria<Site>()
.Add(SqlExpression.Like<Site>(g => g.URLName, host))
.List<Site>()
.FirstOrDefault();
}
I think you can put a .List<Site>() on the end, and then do the .FirstOrDefault() on it.
I believe .UniqueResult() is what you're after...
from the docs:
Convenience method to return a single
instance that matches the query, or
null if the query returns no results.
You can use linq with NHibernate. It is in the NHibernate.Linq namespace in the trunk of NHiberante.
return session.Query<Site>().FirstOrDefault(site => site.UrlName.Contains(host));
When you prefer the criteria API over Linq, you have to use result.SetMaxResults(1).UniqueResult() to create something equal to IQueryable.FirstOrDefault