LINQ - How to use Contains in Linq. - Asked By Sameer Khan on 26-Mar-12 06:16 AM

Hi,
Im working in asp.net MVC3.My code is in C#.Net.

Below is my query which is in LINQ. But i have three queries which i want to make one and get my output...

There are 3 table.
Im displaying the Data of the Master table in the index view.
There is second table which is Details table. This Details table has a root_Id column. Which integer values.
This root_Id value comes from another table called app_Mapping table.
In the app_Mapping table say i have two columns which are Value and root_Id. root_Id is the primary key of  app_Mapping  table...

Sample Data for app_Mapping Table...

root_Id       Value
1               Client

Now i want a query which should give me those records which have the Value ="Client" in the app_Mapping  table.
root_Id is in Details table...Details table also contains the Primary key of Master table.
Please guide


 
 

 
Web Star replied to Sameer Khan on 26-Mar-12 06:28 AM
Simply use the regular String.Contains method on it:
Where e.POSITION.Contains("A[FGL]7")
Or you can also use as follows
var abc=ctx.TableName.Where(f => f.ColumnName.Contains(prefixText)).Select();

hope this helps you
Somesh Yadav replied to Sameer Khan on 26-Mar-12 06:33 AM
hi,
a sample example,

Project your Tasks to a TaskName then use contains on that.

var query = session
   
.Query<Course>()
   
.Where(x => x.Tasks
                 
.Select(t => t.TaskName)
                 
.Contains(myTaskName)
             
&& x.CourseId == 1);
Devil Scorpio replied to Sameer Khan on 26-Mar-12 04:44 PM
Hi,

LINQ supports a Contains extension method which operates on IEnumerable type data.

In the example given below, the Contains extension method is used on the List to search a particular string. A drawback of using LINQ extension method is that the execution is slow. In the query below, the data is in List collection. All cities with the names containing the string ‘San’ are returned.

Example
List cities = new List();     
    cities.Add("New Delhi");              
    cities.Add("Bangalore");               
    cities.Add("Hyderabad");              
    cities.Add("Bombay");   
    
var query = from c in cities
            where c.Contains("Ban")
            select c;
foreach (var row in query)
{         
    Console.WriteLine(row);        
}