IF condition in Linq - linq

how to use If condition in this LINQ
public Student SearchStudentStatus()
{
students=from stud in students
let status=(stud.Status=="PASS")?true:false
return students;
}
If the status is true(PASS), then check for Student's birthday, if it is today then take it otherwise don't take it

So you want to show only students which status is "PASS" and which have birthday today?
public List<Student> SearchStudentStatus()
{
return students
.Where(s => s.Status == "PASS" && s.Birthday.Date == DateTime.Today)
.ToList();
}

Related

Pass multiple parameters in web API GET method

I've the following piece of code to return a list of products based on category.
public IEnumerable<Product> GetProductsByCategoryId(string category_id)
{
return repository.GetAll().Where(
p => string.Equals(p.Category, category_id, StringComparison.OrdinalIgnoreCase));
}
I want to pass two parameters in the method, category and Brand.
public IEnumerable<Product> GetProductsByCategoryBrand(string category, string Brand)
{
}
What would the return method look like with category and Brand?
public IEnumerable<Product> GetProductsByCategoryBrand(string category, string Brand)
{
return repistory.GetAll().Where(
p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase)
&& string.Equals(p.Brand, brand, StringComparison.OrdinalIgnoreCase));
}
You just chain your conditions to the Where() using the && operator. If you want to match either brand or category, use ||

NHibernate Hitting database once for each record in query

I'm measuring database calls on a slow site using NHibernate profiler, and have noticed immediately that the following is causing a Select N+1 issue.
I've never used NHibernate so was hoping someone could help me out?
public virtual IQueryable<Employee> Employees()
{
return Session.Query<Employee>();
}
public IList<Employee> GetEmployeesByClientId(int clientId)
{
return Employees()
.Where(e => e.ClientId == clientId && e.Deleted == false)
.ToList();
}
At the point of calling ToList() a select statement is ran for every related record of EmployeeDetail, and I'm not sure why.
public virtual EmployeeDetail EmployeeDetail { get; set; }
You can use Fetch method to force a join sql statement to fill the property, for sample:
return Employees()
.Fetch(x => x.EmployeeDetail)
.Where(e => e.ClientId == clientId && e.Deleted == false)
.ToList();

Query in CodeIgniter with several where clauses

I'm trying to make a query with severel where clauses, but it gives wrong results. I need to see these users which have role_id=1 (that are students), which are not deactivated. I take $this->uri->segment(3) =teacher_id. And the third where clause is that I need only students that studies in the same school as the school of the teacher , which I take with $this->uri->segment(3). My query is:
public function select_students() {
$this->db->select('*');
$this->db->from('users');
$this->db->where('deactivated_at = "0000-00-00 00:00:00" || deactivated_at IS NULL
AND role_id =1 AND school_id = ("SELECT school_id FROM users WHERE user_id=$this->uri->segment(3)")');
$result=$this->db->get();
return $result->result();
}
I found solution:
public function select_students() {
$this->db->select('*');
$this->db->from('users');
$this->db->where('(users.deactivated_at = "0000-00-00 00:00:00" OR users.deactivated_at IS NULL) AND users.role_id = 1 ');
$this->db->where('users.school_id',$this->uri->segment(4) );
$result=$this->db->get();
return $result->result();
}
better use custom SQL and after that wrap with CI method. This will provide you flexibility to write any custom query as well as protect query with CI security.
public function select_students() {
$sql = SELECT * FROM users WHERE deactivated_at = "0000-00-00 00:00:00" OR deactivated_at IS NULL AND role_id =1 AND school_id = (SELECT school_id FROM users WHERE user_id= (int) $this->uri->segment(3));
$query = $this->db->query($sql);
return $query->result();
}

How to get list that is the distinct select of other lists (LINQ)?

Sorry about the question, I couldn't build the sentence. Here is what I have,
class Brand{
int ModelId;
string name;
}
class Gallery{
IList<Brand> brands;
...
public BrandList{
get{ return brands; }
}
}
I have a list of Gallery. Like this,
IList<Gallery> galleries;
and each Gallery in galleries have many Brands in it. For example, galleries have 6 Gallery object in it. And each gallery has Brands in it. Like this,
Gallery1.Brandlist => Audi, Ford
Gallery2.BrandList => Mercedes,Volvo
Gallery3.BrandList => Subaru
Gallery4.BrandList => Renault
Gallery5.BrandList => Subaru
Gallery6.BrandList =>
What I am trying to get with LINQ is a list of Brands that are distinct of all above's first brand only(so I won't take Ford and Volvo even they are in the list). A gallery doesn't have to have a Brand in their list. So it might be empty as Gallery6. The Output should be,
{Audi, Mercedes, Subaru, Renault}
I don't know how I can do this with LINQ. I tried SelectMany but all I can do with LINQ is simple (p=>p.Something = (int) something).ToList(). I couldn't figure out how to do it.
Use SelectMany and Distinct:
IEnumerable<string> allUniqueBrands = allGalleries
.SelectMany(g => g.BrandList.Select(b => b.Name)).Distinct();
In query syntax:
IEnumerable<string> allBrands = from gallery in allGalleries
from brand in gallery.BrandList
select brand.Name;
IEnumerable<string> allUniqueBrands = allBrands.Distinct();
Edit: Now i got it, you need only the first brands of each BrandList.
If you want to select the Brand you have to provide a custom IEqualityComparer<Brand> which you can use in Distinct. If you neeed a List<Brand>, just call ToList() at the end.
Here's an IEqualityComparer<Brand> for Distinct (or Union,Intesect,Except etc):
public class BrandComparer : IEqualityComparer<Brand>
{
public bool Equals(Brand x, Brand y)
{
if (x == null || y == null) return false;
return x.Name.Equals(y.Name, StringComparison.OrdinalIgnoreCase);
}
public int GetHashCode(Brand obj)
{
if (obj == null) return int.MinValue;
return obj.Name.GetHashCode();
}
}
and here's the distinct list of all (first) brands:
List<Brand> uniqueFirstBrands = allGalleries
.Where(g => g.BrandList != null && g.BrandList.Any())
.Select(g => g.BrandList.First())
.Distinct(new BrandComparer())
.ToList();
This should work:
var brands = galleries.Where(x => x.BrandList.Any())
.Select(x => x.BrandList.First().Name)
.Distinct();
If you want the result being a collection of Brand objects instead of strings, you could do this:
var brands = galleries.Where(x => x.BrandList.Any())
.GroupBy(x => x.BrandList.First().Name)
.Select(g => g.First().BrandList.First());

Deeper level LINQ query in a lambda expression

I would like to get those employees who have a phone number == "666666" using a LINQ query.
These are the class definitions:
public class Employees
{
public List<Phones> Phones{get;set}
}
public class Phones
{
public string Id{get;set;}
public string Number{get;set;}
}
This is my query (my doubt indicated as ???):
var employees= data.GetEmployees()
.Where(e=> e.Phones ???i need to navigate a level below phones ???)
.Select(e => new Employee()
{
Id=e.Id,
Name=e.Name
});
My problem is I don't know how to go a deeper level in this LINQ expression, because in e=>e... I have access to Phones as an IEnumerable but I would like to navigate to Phone's properties.
The easiest way to do this is to use nested LINQ queries. In this case you should look at the Any method.
var employees= data
.GetEmployees()
.Where(e => e.Phones.Any(p => p.Number == "666666"))
.Select(e => new Employee() {
Id = e.Id,
Name = e.Name
});
The parameter passed to the where method is merely a function that returns true or false for each given element, all methods (including LINQ ones (subject to accessing ref/out params etc)) can still be called within it:
var employees= data.GetEmployees()
.Where(e => e.Phones.Any(p => p.Number == "666666"))
.Select(e => new Employee()
{
Id=e.Id,
Name=e.Name
});
var employees= data.GetEmployees()
.Where(e=> e.Phones.Contains(x=>x.Number == "666666"))
.Select(e => new Employee()
{
Id=e.Id,
Name=e.Name
});

Resources