1

I have an Linq appliacation and I need to get a dynamic where expression. I use class:

public class EntityColumnsField
{
    public String ColumnName { get; set; }
    public Type ColumnType { get; set; }
    public bool IsPK { get; set; }
    public String TableName { get; set; }
    public Type TableType { get; set; }
}

I get list of columns of Entity by method:

public static IEnumerable<EntityColumnsField> GetAllColumnsFromEntity(params EntityObject[] entities)
    {
        if (entities == null || entities.Count() == 0)
            throw new ArgumentNullException("entity");

        List<EntityColumnsField> ColumnList = new List<EntityColumnsField>();

        foreach (var entity in entities)
        {
            ColumnList.AddRange(from p in entity.GetType().GetProperties()
                                where p.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false).Any()
                                select new EntityColumnsField()
                                {
                                    TableName = entity.GetType().Name,
                                    ColumnName = p.Name,
                                    ColumnType = p.PropertyType,
                                    IsPK = p.GetCustomAttributes(false).Where(a => a is EdmScalarPropertyAttribute && ((EdmScalarPropertyAttribute)a).EntityKeyProperty).Count() > 0
                                });
        }
        return ColumnList.OrderBy(a => a.TableName);
    }

Than i have 3 tables (User, UserPartner and UserFriends) and I need generate where conditions for all string fields.. I trying do that by this:

using (var db = new DB())
            {
                var ll = from x in db.Users 
                         join y in db.UserPartners on x.ID equals y.ID
                         join z in db.UserFriends on x.ID equals z.ID
                         select new { Users = x, UserPartners = y, UserFriends = z };
            }
if (!String.IsNullOrEmpty(fulltext))
{
var AllSearchField = Utils.GetAllColumnsFromEntity(new User(), new UserPartner(), new UserFriends());
//TODO:
//Here i need a code, which generate predicate for all text fields in tables
//the result would be like :
//ll.Where(a => a.Users.Address.Contains(fulltext) || a.Users.Email.Contains(fulltext) || a.UserPartners.Email.Contains(m.FullText))
}

Has anyone idea how to do this? Thanks

David
  • 15,894
  • 22
  • 55
  • 66
Davecz
  • 1,199
  • 3
  • 19
  • 43
  • looks like you're trying to implement full text search, have you considered existing implementations - like sql server full text search or lucene? – Giedrius Jun 14 '13 at 08:00
  • you may need something like this, foreach (var source in ll.Where(a => a.Users.Address.Contains(fulltext) || a.Users.Email.Contains(fulltext) || a.UserPartners.Email.Contains(m.FullText))) { // do something with source } – Jegan Jun 14 '13 at 08:00
  • Maybe it can help u; http://stackoverflow.com/questions/2455659/how-to-use-contains-or-like-in-a-dynamic-linq-query – CocLn Jun 14 '13 at 08:08
  • I need to be programmed dynamically and only using. NET framework. This is just an example and as a result it has only fulltext search text field independently of the entity. – Davecz Jun 14 '13 at 08:17

3 Answers3

1

You can try Dynamic Linq.

NuGet: https://www.nuget.org/packages/System.Linq.Dynamic

The ScottGu Example: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Kazi Manzur Rashid
  • 1,544
  • 13
  • 14
1

Try this way :

System.Linq.Dynamic 1.0.0

This is the Microsoft assembly for the .Net 4.0 Dynamic language functionality.

To install System.Linq.Dynamic, run the following command in the Package Manager Console

PM> Install-Package System.Linq.Dynamic
Bhavin
  • 27,155
  • 11
  • 55
  • 94
Jignesh.Raj
  • 5,776
  • 4
  • 27
  • 56
0

you may need something like this,

var ll;
using (var db = new DB())
{
    ll = from x in db.Users 
             join y in db.UserPartners on x.ID equals y.ID
             join z in db.UserFriends on x.ID equals z.ID
             select new { Users = x, UserPartners = y, UserFriends = z };
}

if (!String.IsNullOrEmpty(fulltext))
{
    var AllSearchField = Utils.GetAllColumnsFromEntity(new User(), new UserPartner(), new UserFriends());
    //TODO:
    //Here i need a code, which generate predicate for all text fields in tables
    //the result would be like :
    foreach (var source in ll.Where(a => a.Users.Address.Contains(fulltext) || a.Users.Email.Contains(fulltext) || a.UserPartners.Email.Contains(m.FullText)))
    {
        // do something with source    
    }
}

you could also use the FindAll function

foreach (var source in ll.FindAll(a => a.Users.Address.Contains(fulltext) || a.Users.Email.Contains(fulltext) || a.UserPartners.Email.Contains(m.FullText)))
    {
        // do something with source    
    }
Jegan
  • 1,227
  • 9
  • 16