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 Query
- 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
More Query from same tag
- How does Linq-to-Xml convert objects to strings?
- Where clause using Expression <Func<>> and <T>
- Analyzing a Linq expression
- How to await Parallel Linq actions to complete
- Using LINQ, get a list of objects that contain a particular property value
- Identifying duplicate datatable rows using LINQ
- LINQ Select from dynamic tableName string
- Extract keys from Dictionary where all the values in the value are equal to searched value
- Efficient way to get previous n day by date data from LINQ/EF
- LINQ GroupJoin to IGrouping
- How to do Intersect on list of strings created from String.Split
- Dictionary<> value count c#
- Linq subset from two lists of longs
- LINQ - How to change value in select or foreach loop?
- How to optimize a code using DataTable and Linq?
- How to enable the user to arbitrarily query IEnumerable collections?
- Getting count with NHibernate + Linq + Future
- interface design with Linq2Nibernate and IQueryable
- Convert RowData into GridView Columns through LINQ
- Fetching records by date with only day part comparison using nhibernate
- How to order obj based on their hierachy?
- Split DataTable Values(Comma Separated Values) Into multiple rows
- Linq method equivalent for "Into"
- System.OutOfMemoryException on Linq Joins
- Invalid Cast when calling Stored Procedure from Linq-to-SQL
- LINQ: how to add multiple variables to LINQ query?
- Distinct List(Of String) from two Lists
- How do I return all order items for all orders?
- Non Linq way to check list items are same
- Sort datatable on occurrence of string