简体   繁体   中英

dynamic linq where expression

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

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

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    
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM