score:1

Accepted answer

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();
    }

Related Query

More Query from same tag