Reputation: 482
I know the title is somewhat complicate but let I clear the title by explaining the problem.
As per image I want to filter product name in search textbox. For ex: In search textbox if I enter the oil that in datatable whole data want to show which product name is oil.
For filter I used linq query, here is my code,
var dataList = (from x in query
select new
{
PartName = _Db.Part.Where(z => z.Id == x.Select(p => p.PartId).FirstOrDefault()).Select(p => p.Name),
ManufacturerName = _Db.Manufacture.Where(z => z.Id == x.Select(p => p.ManufacturerId).FirstOrDefault()).Select(p => p.name),
CategoryName = _Db.Category.Where(z => z.Id == x.Select(p => p.CategoryId).FirstOrDefault()).Select(p => p.Name),
Pcs = x.Sum(o =>o.Pcs) -
(from m in _Db.MaterialRecord
join s in _Db.ServiceJob on m.ServiceJobId equals s.Id
where m.pid == x.Select(p => p.PartId).FirstOrDefault()
select m).Sum(z => z.Qty),
Weight = _Db.Purchase.Where(p => p.Weight == x.Select(s => s.Weight).FirstOrDefault()).Select(a => a.Weight).FirstOrDefault(),
WeightType = x.Select(p => p.WeightTypeId).FirstOrDefault() > 0 ?((WeightType)x.Select(p => p.WeightTypeId).FirstOrDefault()).ToString() :"",
}).ToList();
//Search
if (!string.IsNullOrEmpty(searchValue))
dataList = dataList.Where(m => m.CategoryName.Contains(searchValue.ToString().ToLower())).ToList();
//Returning Json Data
return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data });
As per code I am getting whole data in datalist variable. Now, when I search any product in searchbox at that time the condition of search will true and after that the datalist value will be null[as shown by debug the code].
So, hence from that datatable shows null data.
For understanding that why this happening I add the tostring() in datalist query of category line i.e.
CategoryName = _Db.Category.Where(z => z.Id == x.Select(p => p.CategoryId).FirstOrDefault()).Select(p => p.Name).toString(),
After adding toString() in this line it shows this line Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable. But I want the result value of this line. For more clear lets see the another image
That oil result value I want in search condition line i.e.
dataList = dataList.Where(m => m.CategoryName.Contains(searchValue.ToString().ToLower())).ToList();
but right now in it is showing this line Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable in the place of result value.
Upvotes: 2
Views: 12602
Reputation: 24957
First thing you should know is ToString()
by its default implementation returns fully-qualified name of the object when applied to collection objects, in this case returns Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<string>
which is a reference type that doesn't override that method.
Let's evaluate this expression first:
CategoryName = _Db.Category.Where(z => z.Id == x.Select(p => p.CategoryId).FirstOrDefault())
.Select(p => p.Name),
Assumed that Category
has DbSet<Category>
type, the Select()
extension method used in code above returns EntityQueryable<string>
which contains result set collection from executed SQL statement provided by LINQ-to-Entities query. If you want to pull single string value from that collection, you must use one of the First()
, Single()
, FirstOrDefault()
or SingleOrDefault()
extension methods.
Hence, you should add extension method as mentioned above to return a string value instead of a collection:
var dataList = (from x in query
select new {
// other properties
CategoryName = _Db.Category.Where(z => z.Id == x.Select(p => p.CategoryId).FirstOrDefault())
.Select(p => p.Name).SingleOrDefault(),
// other properties
}).ToList();
Then you can use Contains
against a substring (searchValue
) because CategoryName
has string type:
dataList = dataList.Where(m => m.CategoryName.Contains(searchValue.ToString().ToLower())).ToList();
Reference:
Entity Framework/Core and LINQ to Entities Query Methods (Operators)
Upvotes: 2