LINQ - optional/nullable where conditions - linq

I do have the following LINQ query, selecting movies from my database that either contain the given search string or have one or more of the tags that I give to the function.
The problem is, that if the search string or the tags parameter are empty/null, I get no movies. The desired action however, is to get all the movies, so, if one parameter is null or empty, I don't want to apply it.
How can I do that?
public IEnumerable<Group<Genre, Movie>> GetMoviesGrouped(string search, List<Tag> tags)
{
var movies = from movie in Movies
where ( movie.Name.Contains(search)) && movie.Tags.Any(mt => tags.Any(t => t.ID == mt.ID))
group movie by movie.genre into g
select new Group<Genre, Movie> { Key = g.Key, Values = g };
....
}

Just for readability, I like to do it step by step
var movies = Movies;
if (!string.IsNullOrEmpty(search))
movies = movies.Where(m => m.Name.Contains(search));
if (tags != null && tags.Any()) {
var tagIds = tags.Select(m => m.ID);
movies = movies.Where(m => m.Tags.Any(x => tagIds.Contains(x.ID));
}
var result = from movie in movies
group ...

You can do something like this to skip the check if nothing is provided:
where (string.IsNullOrEmpty(search) || movie.Name.Contains(search)) &&
(!tags.Any() || movie.Tags.Any(mt => tags.Any(t => t.ID == mt.ID)))

Related

How to Convert a LINQ Query Syntax to a Method Syntax? (with join and multiple selects) Issue: Where-no overloads for + 3 parameters

I am trying to understand lambda expressions with Linq but i struggle with the conversion. Below is the Linq Query syntax which works just fine.
var systemUsersPoor =
(from customer in customers
join distributor in distributors
on new { customer.Location }
equals new{ distributor.Location }
where customer.Location == "UK" &&
customer.Location==distributor.Location &&
customer.Supplier == "MoneyShits" &&
distributor.Products == "ShittyCrappyCraps"
orderby customer.Name
select new
{
customerName=customer.Name , customerLocation=customer.Location, customerSupplier=customer.Supplier,
distributorName=distributor.Name, distributorProducts=distributor.Products
}).ToList();
And then in here, i have my failed attempt to convert it into a Linq Method Syntax...All works untill the .Where statement..states that it does have no definition for my fields(.location,.Supplier) and distributor
var sysUserPoor2 = customers.Join //from customer in customers Join
(
distributors, //Join on distributors on customer.Location==distribution.Location
customer=> customer.Location, //Select the primary key (the first part of the "on" clause in an sql "join" statement
distributor =>distributor.Location, // Select the foreign key (the second part of the "on" clause)
(customer, distributor) => new //select statement
{
customerName = customer.Name,
customerLocation = customer.Location,
customerSupplier = customer.Supplier,
distributorName = distributor.Name,
distributorProducts = distributor.Products
}
)
.Where
(
customer => (customer.customerLocation == "UK") &&
(customer.customerSupplier == "MoneyShits"),
distributor => distributor.distributorProducts == "ShittyCrappyCraps",
(customer, distributor) => (customer.customerLocation == distributor.Location)
);
The query with the code below works, but i dont know how to add the rest somehow...:
.Where
(
customer => (customer.customerLocation == "UK") &&
(customer.customerSupplier == "MoneyShits")
)
Please try following.
var systemUsersPoor = customers.Join(distributors,
customer => customer.Location,
distributor => distributor.Location,
(customer, distributor) => new {customer, distributor})
.Where(x => x.customer.Location.Equals("UK") &&
x.customer.Location.Equals(x.distributor.Location) &&
x.customer.Supplier.Equals("MoneyShits") &&
x.distributor.Products.Equals("ShittyCrappyCraps"))
.OrderBy(x => x.customer.Name)
.Select(x => new
{
customerName = x.customer.Name,
customerLocation = x.customer.Location,
customerSupplier = x.customer.Supplier,
distributorName = x.distributor.Name,
distributorProducts = x.distributor.Products
}).ToList();

LINQ group by ID - Distinct by order

I have data in a generic list named "Assignment" as shown in the Original table. Want to group by ID and display only one record based on US-UK-India order.
The result should transform from first table to second as shown in the attached image.
Try this:
SQL like syntax.
from a in Assignments
group a by a.Id into gr select gr.FirstOrDefault()
Lambda expression:
Assignments
.GroupBy (a => a.Id)
.Select (gr => gr.FirstOrDefault ())
Try this, its works. I hope this is what you need.
var query01 = context.FirstTable
.GroupBy(p => p.ID
,(ky, el) => new { ky.Value , FirstCountry = el.Select(p => Country).Take(1)});
var Result = query01
.Join(context.FirstTable
, ul => new
{
Id = ul.Value
,Country = ul.FirstCountry.FirstOrDefault()
}
, fl => new
{
Id = (int)fl.ID
, Country = fl.Country
}
, (ul, fl) => new { fl }).ToList();

How to write this LINQ with foreach in a better way

I was doing project in MVC3 with Entity framework. I have a LINQ query with foreach. Everything is fine. But when the data size goes up, i was facing performance issues. I dont have much experience with LINQ. So I couldn't fix my issue. Pls have a look at my code and provide a better suggestion for me.
Code
List<int> RouteIds = db.Cap.Where(asd => asd.Type == 3).Select(asd => asd.UserId).ToList();
var UsersWithRoutingId = (from route in db.RoutingListMembers
where RouteIds.Contains(route.RoutingListId.Value) && route.User.UserDeptId == Id
select
new RoutingWithUser
{
UserId = route.UserId,
RoutingId = route.RoutingListId
});
var ListRouteValue = (from cap in db.CareAllocationPercents
where cap.Type == 3
select new UserWithDeptId
{
Year = (from amt in db.CareAllocations where amt.CareItemId == cap.CareItemId select amt.Year).FirstOrDefault(),
UserId = cap.UserId,
UserDeptId = (from userdept in db.Users where userdept.Id == cap.UserId select userdept.UserDeptId).FirstOrDefault(),
});
List<UserWithDeptId> NewRouteList = new List<UserWithDeptId>();
ListRouteValue = ListRouteValue.Where(asd => asd.Year == Year);
foreach (var listdept in ListRouteValue)
{
foreach (var users in UsersWithRoutingId)
{
if (users.RoutingId == listdept.UserId)
{
UserWithDeptId UserwithRouteObj = new UserWithDeptId();
UserwithRouteObj.UserId = users.UserId;
UserwithRouteObj.Year = listdept.Year;
UserwithRouteObj.UserDeptId = db.Users.Where(asd => asd.Id == users.UserId).Select(asd => asd.UserDeptId).FirstOrDefault();
NewRouteList.Add(UserwithRouteObj);
}
}
}
NewRouteList = NewRouteList.Where(asd => asd.UserDeptId == Id).ToList();
Thanks,
You have to use join in first statement. Examples of how to do this are for example here: Joins in LINQ to SQL
I have some idea for you:
First:
Take care to complete your where close into your linq query to get just what you need to.
With Linq on collection, you can remove one foreach loop. I don't know the finality but, i've tryied to write something for you:
var UsersWithRoutingId = (from route in db.RoutingListMembers
where RouteIds.Contains(route.RoutingListId.Value) && route.User.UserDeptId == Id
select
new RoutingWithUser
{
UserId = route.UserId,
RoutingId = route.RoutingListId
});
var ListRouteValue = (from cap in db.CareAllocationPercents
where cap.Type == 3
select new UserWithDeptId
{
Year = (from amt in db.CareAllocations
where amt.CareItemId == cap.CareItemId && amt.Year == Year
select amt.Year).FirstOrDefault(),
UserId = cap.UserId,
UserDeptId = (from userdept in db.Users
where userdept.Id == cap.UserId && userdept.UserDeptId == Id
select userdept.UserDeptId).FirstOrDefault(),
});
List<UserWithDeptId> NewRouteList = new List<UserWithDeptId>();
foreach (var listdept in ListRouteValue)
{
var user = UsersWithRoutingId.Where(uwri => uwri.RoutingId == listdept.UserId).FirstOrDefault();
if (user != null)
{
NewRouteList.Add(new UserWithDeptId { UserId=user.UserId, Year=listdept.Year, UserDeptId=listdept.UserDeptId });
}
}
return NewRouteList
Is that right for you ?
(i don't poll the db.user table do get the UserDeptId for the NewRouteList assuming that the one in the listdept is the good one)
Second:
Take care of entity data loading, if you have tables with foreign key, take care to remove the lazy loading if you don't need the children of your table to be loaded at same time. Imagine the gain for multiple table with foreign key pointing to others.
Edit:
Here is a link that explain it:
http://msdn.microsoft.com/en-us/library/vstudio/dd456846%28v=vs.100%29.aspx

Linq select subquery

As the title states, I'm trying to perform a select subquery in Linq-To-SQL. Here's my situation:
I have a database view which returns the following fields:
SourceId
LicenseId
LicenseName
CharacteristicId
CharacteristicName
Now I want to be able to store this in a model of mine which has the following properties
Id
Name
Characteristics (this is List which has Id, Name and Icon => Icon is byte[])
Here's the query I wrote which doesn't work:
var licensesWithCharacteristics =
_vwAllLicensesWithAttributesAndSourceIdRepository.GetAll()
.Where(x => x.SourceID == sourceId)
.Select(a => new LicenseWithCharacteristicsModel()
{
LicenseId = a.LicenseId,
LicenseName = a.LicenseName
,CharacteristicList = _vwAllLicensesWithAttributesAndSourceIdRepository.GetAll()
.Where(x => x.LicenseId == a.LicenseId)
.Select(c => new CharacteristicModel { Id = c.CharacteristicID, Name = c.CharacteristicName, Icon = c.Icon })
.Distinct().ToList()
})
.Distinct().ToList();
How would you solve this? I'm trying to do this in one query to keep my performance up, but I'm kind of stuck.
Your sample query and models are not that coherent (where does Icon come from, Characteristics or CharacteristicList), but anyway.
I do this in two parts, you can of course regroup this in one query.
I enumerate the result after the grouping, you may try to do without enumerating (all in linq to sql, but not sure it will work).
var groupedResult =
_vwAllLicensesWithAttributesAndSourceIdRepository.GetAll()
.Where(x => x.SourceID == sourceId)
.GroupBy(m => new {m.LicenseId, m.LicenseName})
.ToList();
var results = groupedResult.Select(group => new LicenseWithCharacteristicsModel {
LicenseId = group.Key.LicenseId,
LicenseName = group.Key.LicenseName,
Characteristics = group.Select(m=> new CharacteristicModel {
Id = m.CharacteristicId,
Name = m.CharacteristicName
}).ToList()
});
in "single query"
_vwAllLicensesWithAttributesAndSourceIdRepository.GetAll()
.Where(x => x.SourceID == sourceId)
.GroupBy(m => new {m.LicenseId, m.LicenseName})
.Select(group =>
new LicenseWithCharacteristicsModel
{
LicenseId = group.Key.LicenseId,
LicenseName = group.Key.LicenseName,
Characteristics = group.Select(m =>
new CharacteristicModel
{
Id = m.CharacteristicId,
Name = m.CharacteristicName
}).ToList()
});

Access any data that is not contained in grouped element

from teamBudget in TeamBudgets
where teamBudget.TeamID == 71002
join teamBroker in TeamBrokers on 71002 equals teamBroker.TeamID
join goal in Goals on teamBroker.GlobalBrokerID equals goal.GlobalBrokerID
group goal by goal.GlobalBrokerID into g
select new
{
// TeamID=teamBroker.TeamID,
// MTDGoal=teamBudget.Sum(t => t.Budget),
RevenueMTDCurrent = g.Sum(x => x.RevenueMTDCurrent)
}
Commented part is a problem. How to access any data that is not contained in grouped element?
you need to Group multiple fields then only you can access that data.
like
var result = from i in
(from uh in db.UserHistories
where uh.User.UserID == UserID && uh.CRMEntityID == (int)entity
select new { uh.ActionID, uh.ActionType, uh.ObjectID })
group i by new { i.ActionID, i.ActionType, i.ObjectID } into g
select new { g.ActionID, g.ActionType, g.ObjectID };
Hope this will help

Resources