Dynamic Linq Search using NinjaNye.SearchExtensions - c#

I use the great NinjaNye.SearchExtensions in c# core mvc. That works perfect but I would need dynamic search options. Is that possible? Or do I need several if else blocks?
I have this linq
return tmpIQueryable
.Where(c => data.Contains(c.id) && c.enddate > DateTime.Now.Date && c.startdate < untildate)
.Include(c => c.DataContainer)
.Search(x => x.title.ToLower(),
x => x.manu.ToLower(),
x => x.short.ToLower(),
x => x.long.ToLower(),
x => x.detail.ToLower())
.ContainingAll(searchlist)
I have boolean variables where to search in (user can choose in GUI with checkboxes where he want to search in) - search_in_title, search_in_manu, search_in_short, search_in_long, search_in_detail. How can I dynamic search in a field or not without in this case 5^2 if/else for different linq.
Thanks a lot
Ralf

You may add .Where clause multiple times:
if (searchInTitleChecked)
tmpIQueryable = tmpIQueryable.Where(r => r.Title.ToLower() == title);
if (searchInShortChecked)
tmpIQueryable = tmpIQueryable.Where(r => r.Short.ToLower() == short);
This is just example of Linq, but not NinjaNye.SearchExtensions. If it's not helpful, please ignore.
However you may try the same here:
if (searchInTitleChecked)
tmpIQueryable = tmpIQueryable.Search(x => x.title.ToLower());
if (searchInShortChecked)
tmpIQueryable = tmpIQueryable.Search(x => x.short.ToLower());
tmpIQueryable = tmpIQueryable.ContainingAll(searchlist);

Related

Is there a way to simplify these linq statements using an .Include()?

Currently I am doing a keyword search on the Plates table (Name column) but also have a Search (searching on SearchTerm column) table which contains Plat Id's that I also want to search and return the corresponding platforms.
The code below works but I'd like to simplify the logic using an .Include statement if possible although I'm not quite sure how. Any help would be greatly appreciated.
if (!string.IsNullOrEmpty(request.Keyword))
{
var searchTermPlateIds = await _db.Search
.Where(x=> x.SearchTerm.ToLower().Contains(request.Keyword.Trim().ToLower()))
.Select(x => x.PlatformId)
.ToListAsync(ct);
var plateFromPlateIds = await _db.Plate
.OrderBy(x => x.Name)
.Where(x => searchTermPlateIds.Contains(x.Id) && x.Status != PlateStatus.Disabled)
.ToListAsync(ct);
plates = await _db.Plates
.OrderBy(x => x.Name)
.Where(x => !string.IsNullOrEmpty(request.Keyword.Trim()) && x.Name.ToLower().Contains(request.Keyword.Trim().ToLower()) && x.Status != PlateStatus.Disabled)
.ToListAsync(ct);
plates = plates.Union(platesFromPlateIds).ToList();
}
Remember simple thing, Include ONLY for loading related data, not for filtering.
What we can do here - optimize query, to make only one request to database, instead of three.
var query = _db.Plates
.Where(x => x.Status != PlateStatus.Disabled);
if (!string.IsNullOrEmpty(request.Keyword))
{
// do not materialize Ids
var searchTermPlateIds = _db.Search
.Where(x => x.SearchTerm.ToLower().Contains(request.Keyword.Trim().ToLower()))
.Select(x => x.PlatformId);
// queryable will be combined into one query
query = query
.Where(x => searchTermPlateIds.Contains(x.Id);
}
// final materialization, here you can add Includes if needed.
var plates = await query
.OrderBy(x => x.Name)
.ToListAsync(ct);

QueryOver in NHibernate

I'm lost when it comes to to QueryOver in NHibernate, I'm trying to query over a database and retrive 4 values of importans, the rest are unnecessary and take up processing power.
I'm trying this:
var ext = _session.QueryOver<ExternServiceSettings>()
.Where(x => x.ExternService == ExternServiceEnum.Outlook).List();
which works fine but takes too long and returns everything in the database. then I tried:
var ext = _session.QueryOver<ExternServiceSettings>()
.Where(x => x.ExternService == ExternServiceEnum.Outlook)
.List<ExternServiceSettings>()
.Select(y => y.UserName);
However this only return the username and won't let me fetch more than one value...
All help is appreciated!
We should use .SelectList()
Check the example from doc:
var selection =
session.QueryOver<Cat>()
.SelectList(list => list
.Select(c => c.Name)
.SelectAvg(c => c.Age))
.List<object[]>();
see more here:
16.7. Projections

Linq select with filtering not working

I'm trying to select one field last record in filtered database (this is different than last inserted record). I tried with following code in controller but instead of field value, i'm getting "true" or "false", depending on if there's results after filtering or not.
List<Pozicije> poz = new List<Pozicije>();
poz = db.Pozicijes.Where(p => p.grupa == grupa)
.OrderBy(p => p.sifra_pozicije).ToList();
string pos = poz.Select(p => p.sifra_pozicije.Contains(s)).LastOrDefault().ToString();
can someone point me how to get value i need instead?
Try this instead. I've combined both parts of your query into one.
var pos =
Convert.ToString(db.Pozicijes.Where(p => p.grupa == grupa
&& p.sifra_pozicije.Contains(s))
.OrderByDescending(p => p.sifra_pozicije)
.Select(p => p.sifra_pozicije)
.FirstOrDefault());
If it doesn't work, you may need to tell us what types s and sifra_pozicije are.
LastOrDefault is not supported with LINQ to Entities/LINQ TO SQL. You need to do OrderByDescending and then get First record. Like:
string pos = db.Pozicijes.Where(p => p.grupa == grupa && p.sifra_pozicije.Contains(s)))
.OrderByDescending(p=> p.sifra_pozicije)
.Select(r=> r.sifra_pozicije)
.First();

Linq, unable to list only distinct results

I have three tables, car_type, car_manufacturer and car_model. When the user click on the particular vehicle type they want to browse, I'd like to show them a list of available manufacturers. The problem is the list of manufacturers is not distinct or unique. So if my db has three models from Mazda, Mazda will show up on the list 3 times. This is my controller:
public ActionResult Browse(string click_string)
{
var x = carDB.Models
.Include(b => b.Manufacturer)
.Include(a => a.VehicleType)
.Where(a => a.VehicleType.TypeName == click_string);
return View(x.ToList());
}
How can I write this to remove redundant listings? This is all new to me, so go easy on me.
You have to query for Manufacturers, not for Vehicles:
var x = carDB.Models.Where(a => a.VehicleType.TypeName == click_string)
.Select(a => a.Manufacturer)
.Distinct();
It usually works well to try and avoid Distinct altogether. You want manufacturers? Get manufacturers. And determine from there which ones you need: the ones that produce models that have click_string in their type name:
carDB.Manufacturers.Where(manufacturer => manufacturer.Models
.Any(model => model.VehicleType.TypeName == click_string))
You may want to include Models and/or VehicleType, that depends on what you want to show in the view.
First try doing a .Distinct() at the end of the query, if it does not work you might need to provide a custom comparer for the .Distinct()
You should be able to use .Distinct to return the distinct elements.
var x = carDB.Models
.Include(b => b.Manufacturer)
.Include(a => a.VehicleType)
.Where(a => a.VehicleType.TypeName == click_string)
.Distinct();
add distinct
var x = carDB.Models
.Include(b => b.Manufacturer)
.Include(a => a.VehicleType)
.Where(a => a.VehicleType.TypeName == click_string)
.Select(y => y)
.Distinct();
The .Select() might be a bit verbose but without trying it in my visual studio i put it in there for saftey

LINQ aggregate sequence contains no elements

I'm dynamically building up a query starting with this:
var items = db.Items;
...
case "4":
items = items.OrderBy(x => x.Ratings.Average(t => t.score)).ThenBy(x => x.title);
The problem is that some items don't have any data in the ratings table yet, so I believe it's trying to average over data that doesn't exist. Using DefaultOrEmpty() at the end doesn't seem to have any effect. Any suggestions on how I would fix this?
If you are using this form:
var effectiveFloor =
policies
.Where(p => p.PricingStrategy == PricingStrategy.EstablishFloor)
.Max(p => p.Amount);
Then the solution is:
var effectiveFloor =
policies
.Where(p => p.PricingStrategy == PricingStrategy.EstablishFloor)
.DefaultIfEmpty()
.Max(p => p==null ? 0 : p.Amount);
Found here
items = items.Where(x=>x.Ratings.Any()).
OrderBy(x => x.Ratings.Average(t => t.score)).
ThenBy(x => x.title);
Try that.

Categories