Where clause using Linq - search for a list of Object

c# linq select from list of objects
linq where condition in list c#
how to write linq query in c#
linq select query with where clause in c#
linq query to select data from list
linq update query in c# with where condition
linq query c#
linq select one property from object list

One doctor can work in One hospitals.

The Doctor table looks like this:

Id
Name
Speciality
HospitalName 

HospitalName is a string.

Hospital Table contains the following

Id
HospName 
Address

Now, I have a List of Hospital Objects. Where I need to filter it using a List of Doctor. I need to search from the Doctors table, where the HospitalName is equal to HospName in the Hospital table.

Code:

 List<Hospital> hos = listHospitals;
 var doctors = docList
.Where(h=> listHospitals.Contains(h.HospitalName));

I get an error that states :

Can not convert form string to Hospital`

How can I solve this ?

You can use Any() like below.listHospitals is of type List<Hospital> and thus you will have to query that list to compare hospital name.

List<Hospital> hos = listHospitals;
 var doctors = docList
.Where(h=> listHospitals.Any(x => x.HospitalName == h.HospitalName)).ToList();

Query a collection of objects (LINQ in C#), Learn how query collections using LINQ in C#. This example shows how to perform a simple query over a list of Student objects. For example, you can try more conditions in the where clause, or use an orderby clause to  C# – LINQ Where Examples Where is a LINQ functionality to filter data in a query with given criteria. Each of below examples is presented in C# with both Lambda and Query expression.

Try this...

var list = docList.Where(d => listHospitals.FirstOrDefault(h => d.HospitalName == h.HospName) != null).ToList();

Basic LINQ Query Operations (C#), When you end a query with a group clause, your results take the form of a list of lists. Each element in the list is an object that has a Key member  Each Student object contains some basic information about the student, and a list that represents the student's scores on four examinations. This application serves as the framework for many other examples in this section that use the same students data source.

I'd say to use Count() instead of Any() since a List has a .Length or .Count property thus not need to go through the GetEnumerator()/MoveNext()/Dispose() sequence required by Any().

Resulting in the following code:

List<Hospital> hos = listHospitals;
var doctors = docList
                 .Where(h=> hos.Count(x => x.HospitalName == h.HospitalName) > 0).ToList();

How to Update Members of a Collection with LINQ -- Visual Studio , The code is terser if you'd prefer to use LINQ's Where method and a get an even terser statement by using the List object's ForEach method. By Rod McBride . Usually, when filtering data from a database or a collection, you are comparing one or two values to a list of many. However, with a recent application, I found myself needing to filter a list of items using another list.

Filtering Data Using LINQ, By filtering the data, we can avoid having to search our data set for the the form of a similar WHERE clause and, in the case of Query syntax, a . it would be handy to be able to click on the Location and see a list of who the employees are. For now, we'll say that we want a set of full Employee objects:. 3 Answers3. active oldest votes. 1. You can use Any () like below. listHospitals is of type List<Hospital> and thus you will have to query that list to compare hospital name. List<Hospital> hos = listHospitals; var doctors = docList .Where(h=> listHospitals.Any(x => x.HospitalName == h.HospitalName)).ToList(); share.

Filter list<string> using linq, You're almost there. First you need to identify which records contain userdata. . A where clause helps here: Hide Copy Code. config.Where(x  In LINQ the join clause always works against object collections instead of database tables directly. var innerJoinQuery = from cust in customers join dist in distributors on cust.City equals dist.City select new { CustomerName = cust.Name, DistributorName = dist.Name };

[Solved] How do I return a single value from a list using LINQ , by specifying just the ID in the Select clause, I stop myself being able to reference You could also load the FirstOrDefault into an object and check for null Single expects a single response that has to be there, I find myself Name == fruitName); // LINQ SQL-ish: Fruit f = (from x in ListOfFruits where x. Just change the conditions that you are querying on. I'm converting your FindAll to a Where clause as I know that works for nested queries. I'm also converting this to a method, since as a property the possibility exists that ddlCertificate has a null value, which will cause an exception if the property is called prematurely.

Comments
  • List<Doctor> doctors = docList.Where(doctor => hos.Any(hospital => hospital.HospName == doctor.HospitalName)).ToList();
  • While FirstOrDefault(Predicate) != null is roughly as efficient as Any(Predicate), the latter is far easier to read.
  • This is a very bad idea. If I hand you a stack of playing cards and ask you "are any of them the queen of spades" do you have to count how many queens of spades there are? Of course not. You would not form a list of all of the queens of spades and then count that list. You would enumerate the cards until you found the first queen of spades and then stop. More generally though your argument is nonsense. The fact that list has a Count property does not mean that the filter is O(1) to enumerate! The Count that takes a predicate is more expensive than Any.
  • I am interested to learn how people come to believe false things about programming. Can you explain how it is that you came to believe that Count that takes a predicate could possibly be cheaper than Any that takes a predicate? Count has to do more work than Any so it cannot be cheaper; why did you believe that it was cheaper? I use this information to help write better books, articles and other educational materials.