LINQ, Search sql server table column with contain function - asp.net-mvc

In my Asp.Net Mvc 5 application, I want to search via LINQ query expression, all the SKUs from a SQL Server 2012 table (tbl(sku nvarchar(100))) which contain 'P' and the count of all 'P's should be equal to '5'.
Note: The character e.g 'P' and the count '5' are variables.
I have tried the following so far:
var skus = productiondata.Where(x => x.OrderItemSKU.Contains('P'));

It can easily be done at the client side, but I guess that's not the point. To make it happen in the EF db query, you can use something like this
char ch = 'P';
int count = 5;
var s = ch.ToString(); // important
var skus = productiondata
.Where(x => x.OrderItemSKU.Length - x.OrderItemSKU.Replace(s, "").Length == count);

For your code it should be:
var skus = productiondata.Where(x => x.OrderItemSKU.Count(y => y == 'p') == 5);

Related

How can I get the content by the User Id when using GroupBy in Linq

//Used to get the data I wanted
var MatchResult = (from a in User
join b in UserResult on a.Id equals b.UserId
where a.Status == "Active" select new
{
a.Id,b.Win,b.Lost,b.WinRate
}
//Used to Append the info grouped By UserId
var result = MatchResult.GroupBy(x => x.UserId)
.Select(grp => new { UserId= grp.Key,
UserEmail = grp.Select(x => x.UserEmail ).FirstOrDefault(),
Content = MatchResult.Where(x => x.UserId== grp.Key).ToList() }).ToList();
As the picture shown below, Columns for content is getting a repetitive from userId 1 I try to use read the linq var result in order to sort out the content by Where clause. but turns out I am getting this error message
Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.

Mvc Telerik grid With large database

I am using telerik mvc grid in my mvc project , my table have around 1 Million records. My grid taking too much time to load.
This is my Query
//
var bib = (from a in db.Bibs
join inf in db.InfoTypes
on a.InfoTypeId equals inf.Id
where a.Status == "A"
select new BibViewModel
{
Id = a.Id,
Type = inf.Type,
InfoType = inf.Description,
Title = (from asd in db.BibContents where asd.BibId == a.Id && asd.TagNo == "245" && asd.Sfld == "a" select asd.Value).FirstOrDefault(),
Author = (from asd in db.BibContents where asd.BibId == a.Id && asd.TagNo == "100" && asd.Sfld == "a" select asd.Value).FirstOrDefault(),
CatalogueDate = a.CatalogDate,
Contents = "",
CreatedOn = a.CreatedOn,
ItemRelation = db.Items.Any(item => item.BibId == a.Id),
IssueRelation = db.Issues.Any(item => item.BibId == a.Id),
});
return View(new GridModel(bib.OrderByDescending(x => x.CreatedOn).Tolist()));
ToList() actually invokes the query, so if calling ToList() is taking too long, that means the issue is with the query.
In LINQ, you can use paging like in the following post; the idea is to use Skip and Take to skip X records, and only take Y records, as in:
var results = (from .. select ..).Skip(X).Take(Y)
With 1M records, I would highly suggest replacing it with a stored procedure, which will be much, much faster for what you are trying to do. Consider a custom pagination approach, which works very well for me with large result sets:
http://www.neiland.net/blog/article/dynamic-paging-in-sql-server-stored-procedures/
http://www.beansoftware.com/ASP.NET-Tutorials/Paging-Stored-Procedures.aspx
http://www.sqlpointers.com/2009/10/custom-sorting-and-paging-in-sql-server.html
T-SQL stored procedure with sorting and paging enabled not working properly
If you can't use stored procedures, reading this will help understand what needs to be accomplished with pagination. With LINQ, you'll want to examine the SQL being generated to see where you also can fine-tune the query, either using SQL profiler, or LINQPad.

Entity Framework 4: Best practices in searching?

I am developing a ASP.NET MVC 3 Application with EF4. I have about 50,000 entities and I'm querying them through LINQ to find what matches best with the given search criteria. There are multiple search criterion (up to about 12) and these are matcheon a step by step basis.
Ex: 50,000 students
Get the students within the age range -> A
From A, get the students who are male -> B
From B, get students who are enrolled to course CS101 -> C
What's the best way to achieve this?
step by step doesn't mean a lot for SQL and your linq query will be transformed to sql to query the db...
so
//I'm a IQueryable
var queryableStudents =
students.Where(m =>
m.Age > 10 &&
m.Gender == 'm' &&
m.CourseList.Any(x => x.Name == 'CS101');
//I'm no more an IQueryable
var result = queryableStudents.ToList();//the query will be sent to db and result returned.
But if search criteria are optional, you can do
//I'm a IQueryable
var queryableStudents = students;
if (searchCriteria.Age > 0)
//I'm still a IQueryable
queryableStudents = queryableStudents.Where(m => m.Age => searchCriteria.Age);
if (!String.IsNullOrEmpty(searchCriteria.Gender))
//I'm still a IQueryable
queryableStudents = queryableStudents.Where(m => m.Gender == searchCriteria.Gender);
//Now I'm no more an IQueryable
var result = queryableStudents.ToList()//the query will be sent to db and result returned.
If you want a "REAL" step by step, (showing results for each step), you can do
//I'm not an IQueryable
var a= students.Where(m => m.Age > 10).ToList();//you will get all students from your db who respect your first criterion, and then work on an IEnumerable, not an IQueryable.
//I'm not IQueryable
var b= a.Where(m => m.Gender == 'm');
//I'm not an IQueryable
var c= b.Where(m => m.CourseList.Any(x => x.Name == "CS101");
var A = from s in students
where ((s.age < max) && (s.age > min))
select s;
var B = from a in A
where (a.gender.Equals("Male"))
select a;
var C = from b in B
where (b.EnrolledCourses().Contains("CS101"))
select b;
Answering my own question - after some thought I figured out the most effiecient way of doing this is to use something like ElasticSearch to index the entries I want.
The given use case is not a very good one for LINQ/C#.

How to specify a condition in an Entity Framework join?

I have a Blogs table related to BlogComments table with a FK.
I need to get, through Linq, all the BlogComments items that match a certain flag
If i do:
db.Blogs.Where(b => b.BlogComments.Where(bc=>bc.Where(bc.Flag1==true));
I get "Cannot implicity convert type IEnumerable to bool"
Which is the best way to solve this problem?
Because this expression:
b.BlogComments.Where(...)
returns an IEnumerable (of BlogComments), but you are then passing it into this method:
db.Blogs.Where(...)
which expects a function that returns a bool, not an IEnumerable.
You probably need something like this:
var blogId = 5;
db.BlogComments.Where(bc => bc.BlogId == blogId && bc.Flag1 == true)
If you need to select comments from multiple blogs, then you could try using Contains:
var blogIds = new [] {1,2,3,4,5};
db.BlogComments.Where(bc => blogIds.Contains(bc.BlogId) && bc.Flag1 == true)
If you want to place criteria on the set of blogs, as well as the comments, then you could do this in one query using a join:
var query = from b in db.Blogs
join c in db.BlogComments on c.Blog equals b
where b.SomeField == "some value"
&& c.Flag1 == true
select c;
You could write it in LINQ form.
var blogs = from b in db.Blogs
join c in db.BlogComments
on b.BlogId equals c.BlogId
where c.Flag1
select b;
If you have a composite key you can write
on new { A = b.BlogKey1, B = b.BlogKey2 }
equals new { A = c.CommentKey1, B = c.CommentKey2 }
If it were me, I would just have another DbSet in your DbContext.
DbSet<BlogComment> BlogComments
and just search through there without going through Blogs.
db.BlogComments.Where(bc => bc.Flag1 == true);
If anyone knows if there's anything wrong in doing so, then I'm all ears :)

Linq with EF dynamic search

I am using EF 3.5 with MVC.
I want to made a search page, has some fields for criteria like date, int etc.
What is the way in linq to entities to filter the result dynamically.
If there are one parameter we can use
.where(a=>a.id==1)
but many combination with optional param how can i load results and then pass to model.
EF 3.5? Anyway...
You can append search criteria over an ObjectQuery, ObjectSet or IQueryable and chain them based on which search criteria is useful.
public SearchMyThings( string a, string b, int c )
{
var mywidgets = ObjectContext.CreateObjectSet<Widget>();
//or the EF 1.0 version CreateSet?
if( !a.IsNullOrEmpty )
mywidgets = mywidgets.Where( w => w.AProperty == a );
if( !b.IsNullOrEmpty )
mywidgets = mywidgets.Where( w => w.BProperty == b );
if( c > 0 )
mywidgets = mywidgets.Where( c => c.CProperty == c );
}
If you need a string based approach you can always use the overloads of ObjectQuery.Where("esql") to dynamically construct some eql and passing that along.
If you need MORE control over the strings and aren't afraid of complexity you could give Dynamic Linq a try.

Resources