score:1
You can write such a method by building an expression tree as follows:
Expression<Func<T, bool>> AnyColumnContains<T> (string value)
{
var p = Expression.Parameter (typeof (T), "entity");
var fieldAccessors = typeof (T)
.GetFields()
.Where (f => f.FieldType == typeof (string))
.Select (f => Expression.Field (p, f))
.ToArray();
var fieldArray = Expression.NewArrayInit (typeof (string), fieldAccessors);
var concatCall = Expression.Call (typeof (string).GetMethod (
"Concat", new[] { typeof (string[]) }), fieldArray);
var contains = Expression.Call (
concatCall,
typeof (string).GetMethod ("Contains", new[] { typeof (string) } ),
Expression.Constant (value));
return Expression.Lambda<Func<T, bool>> (contains, p);
}
First, we get all the fields from the entity type (LINQPad uses fields to represent columns; you can change this to GetProperties for DataContexts generated in Visual Studio).
We need to create an expression that feeds these fields into a string.Concat statement. As the latter accepts an array of strings, we create an NewArrayInit expression to build up the array.
Next, we call the Concat method to join the strings together, and finally, the string.Contains method to test whether the string literal is in the expression that we built.
Here's how the method runs on AdventureWorks:
void Main()
{
Addresses.Where (AnyColumnContains<Address> ("Seattle")).Dump();
}
Lambda translation:
Addresses
.Where (
entity =>
String
.Concat (new String[] { entity.AddressLine1, entity.AddressLine2,
entity.City, entity.PostalCode } )
.Contains ("Seattle")
)
SQL translation:
-- Region Parameters
DECLARE @p0 NVarChar(1000) = '%Seattle%'
-- EndRegion
SELECT [t0].[AddressID], [t0].[AddressLine1], [t0].[AddressLine2], [t0].[City],
[t0].[StateProvinceID], [t0].[PostalCode], [t0].[rowguid] AS [Rowguid],
[t0].[ModifiedDate]
FROM [Person].[Address] AS [t0]
WHERE ((([t0].[AddressLine1] + [t0].[AddressLine2]) + [t0].[City]) + [t0].[PostalCode])
LIKE @p0
score:1
You can use Aggregate, if you have each row as an array of the values of the columns. This is the case with DataTable
.
score:1
Try this:
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn { DataType = typeof(int), ColumnName = "A" });
dt.Columns.Add(new DataColumn { DataType = typeof(int), ColumnName = "b" });
dt.Columns.Add(new DataColumn { DataType = typeof(string), ColumnName = "c" });
DataRow r;
for (int i=0;i<=5 ;i++)
{
r = dt.NewRow();
r["A"] = i;
r["b"] = i + 2;
r["c"] = i.ToString();
dt.Rows.Add(r);
}
var query = from DataRow row in dt.Rows.Cast<DataRow>().ToList()
let textUnion = GetFields(row)
select new { textUnion };
dataGridView1.DataSource = query.ToList();
}
string GetFields(DataRow row)
{
StringBuilder retValue=new StringBuilder();
for (int i = 0; i < row.ItemArray.Length;i++ )
{
retValue.Append(Convert.ToString(row[i]));
}
return retValue.ToString();
}
Source: stackoverflow.com
Related Articles
- iterate over fields using linq
- Iterate over strings that ".StartsWith" without using LINQ
- Iterate over a list plus one value using LINQ
- How to iterate over results of LINQ where query results using list<> in C#
- using Linq to iterate over an array and create a new List of objects?
- Convert string[] to int[] in one line of code using LINQ
- update 2 fields using linq foreach
- How can I iterate over a collection and change values with LINQ extension methods?
- How to combine multiple fields into one field using LINQ
- Left outer join using LINQ -- understanding the code
- How to reuse a linq expression for 'Where' when using multiple source tables
- LINQ on a LinkedList - iterate over LinkedListNode<T>, not T
- Avoiding code repetition when using LINQ
- Iterating over class properties using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- LINQ Source Code Available
- How to convert list of objects with two fields to array with one of them using LINQ
- Using LINQ to iterate combinations
- Using Linq and C#, how could I kinda merge two lists over some criteria?
- How can I write the following code more elegantly using LINQ query syntax?
- How can I code an outer join using LINQ and EF6?
- C# .Net 3.5 Code to replace a file extension using LINQ
- Trying to understand LINQ code using c#
- Retrieve bool result by using LinQ code
- creating Linq to sqlite dbml from DbLinq source code
- Using reflection to obtain select fields in a linq query
- read icollection data using LINQ in C# code
- Linq over datatable using dynamic library sample?
- How to show multiple fields in combobox using linq in C# Windows form?
- Linq sub query when using a repository pattern with EF code first
- Correct MS Access Query / Linq to use
- Merge two or more T in List<T> based on condition
- Linq Extension Method - GetYearWeekFormat Extension
- Why would a Database developer use LINQ
- LINQ join with top 1 from another table
- Need Func to supply to Where() method of both IEnumerable and IQueryable
- Dropdownlist Datasource by LINQ Query
- ASP MVC Build Throwing Warning For ApplicationUser Roles Navigation Property?
- LINQ - Dynamic expression to OrderBy
- Read from xml file to objectlist using Linq
- IQueryable failback to IEnumerable
- Linq to Entities group by week number
- Comparing dates in LINQ which are stored as strings in database
- Look for substring in string to find part before searched sentence
- How to read a sitemap using VB.NET
- Grouping by with LINQ
- Using ToList ForEach in an ActionResult to update table values
- Are these improper ways of using LINQ?
- LINQ dynamic method call using expressions
- Initializing a List<T> using an EntityCollection that has a foreign key