动态linq表达式

时间:2013-06-14 07:47:18

标签: c# linq entity-framework

我有一个Linq appliacation,我需要动态表达式。 我用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; }
}

我按方法获取实体列的列表:

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

我有3个表(User,UserPartner和UserFriends),我需要生成所有字符串字段的条件。我试着这样做:

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

有谁知道怎么做? 感谢

3 个答案:

答案 0 :(得分:1)

尝试这种方式:

System.Linq.Dynamic 1.0.0

这是.Net 4.0 Dynamic language功能的Microsoft程序集。

要安装System.Linq.Dynamic,请在Package Manager Console

中运行以下命令
PM> Install-Package System.Linq.Dynamic

答案 1 :(得分:1)

答案 2 :(得分:0)

你可能需要这样的东西,

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    
    }
}

你也可以使用FindAll功能

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    
    }