score:1
Accepted answer
define base query which will return db.if_table
. after that build the query around this result. this 3 if
on your result should cover all of your cases.
iqueryable<if_table> result = db.if_table;
if (!string.isnullorempty(col1))
{
result = result.where(x => x.col1 == col1);
}
if (!string.isnullorempty(col2))
{
result = result.where(x => x.col2 == col2);
}
if (!string.isnullorempty(col3))
{
result = result.where(x => x.col3 == col3);
}
if you want empty datatable
to be return when all col1, col2, col3 are empty string or null. you can define bool variable filltable
and set it to true
in the if statements. after the ifs you can check if(!filltable)return dt;
Source: stackoverflow.com
Related Query
- Linq query based on the condition
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- how to take 100 records from linq query based on a condition
- filter a linq query based on the results of another query's results
- How can I write the following code more elegantly using LINQ query syntax?
- Determine the source DataContext for a Linq to Sql query
- Improve the LINQ query returning indexes of items meeting a certain condition
- LINQ Query - Selecting a key based upon the value having a property that matches a string?
- How can I query the contents of a List via LINQ based on string values?
- How to add conditional condition to LINQ query based on a previously returned value
- Is there a way to add or remove a line of an LINQ to DB Query based on if the value being checked is null?
- Sort a LINQ grouped query based on the group
- LINQ query to return list where condition is based on another table
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How do I determine the source of unpredictable LINQ query results?
- Reduce the line of code for this LINQ query
- How to filter linq query based on all of the values of one property from another list
- Can I Use The Same Linq Code to Query for Different Conditions?
- How to do condition statement in LINQ based on code variables?
- How to write aggregate query in LINQ reusing part of the select code
- How to execute code as a part of the LINQ query
- What SQL query or LINQ code would get the following data?
- How can I build a LINQ predicate/dynamic.LINQ query based off grid filtering when the grid properties don't have the entity properties?
- LINQ query for grouping a table based on the values from another table
- How to get a Linq query to order results based on the order of arguments in a method signature
- Shortcut LINQ toentity query for the following code in MVC 3
- linq query to get multiple datatable from dataset based on some condition
- Linq query to select the records based on the given months for a year
- How to write C# LINQ code to select based on condition
- filtering a linq query, based on the object created from each element in the linq query
More Query from same tag
- c# linq statement to calculate balance over 12 months
- How to query SQLite for whether a record exists?
- Does the order of the include and the where matter in a LINQ query?
- Convert 2D array to string in C#, looking for most elegant way
- How to join sp to table Linq
- What to use with WP8, SQL Compact Edition or Sqlite?
- How to iterate a tree structure with logic on where to stop
- Apply distinct on the basis of item2 of List of integer tuples in c#
- Merging a list into a Dictionary with a dynamic key type
- Different ways to populate a model in MVC C# from a LINQ query?
- Populating LINQ Where statement with List<class>
- Entity Framework - AddOrUpdate using Linq and Lambda
- C# Linq alternative for lodash omit function
- how to write a query with 2 equals on linq?
- Find duplicate objects in list when grouping on one or more fields
- join multiple table with where condition
- How to get unique First Letter of names and count of names starting with that letter from SQL Server using LINQ?
- Using helper methods in Entity Framework query
- Any() linq query on XmlNodeList
- Best way to combine 2 lists
- Using linq group by statement as a subquery
- Linq Count() will always return 1 within Visual Studio
- Convert SQL statement to Linq - tools not working
- mutating the linq query to return the desired results
- Convert DataTable to Linq
- dbmetal visual metal error:"magma string must not be empty"
- Best practice for returning IEnumerables from LINQ Methods
- Linq to group by 2 columns in C#
- What's the lambda expression to check to see if one property in any of a group of objects matches any of a group of strings?
- How to get the sum of multiple columns of a datatable in c#