Reputation: 1221
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
Upvotes: 1
Views: 1586
Reputation: 5987
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
Upvotes: 1
Reputation: 1237
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
}
Upvotes: 0
Reputation: 1544
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
Upvotes: 1