我有一个项目要求我做这么大的搜索引擎,但这一切都是动态的.我的意思是我可以拥有大约0到9个主要的"组",这些组内部的东西就像是"无论是""或"与"或"的无限可能性.我们首先想到的是使用Dynamic Linq,它提供了构建动态查询的良好替代方案.所有这些都使用EF和自制包装.
问题:我无法访问"收藏".我的意思是,我可以伊斯利访问引用的对象(像Customer.State.StateName ="新纽约" OR Custoemr.State.StateName ="魁北克"),但我无法找到一个方法来存取权限是这样的:" Customer.Orders.OrderID = 2 OR Customer.Orders.OrderID = 3".我可以很容易地弄清楚这是因为它是一个集合,但我怎么能这样做呢?
请帮帮我!!
**对不起我的英语不好 !!
更新
我不清楚我是否愿意,对不起因为我是法国人......
我的问题是因为没有什么是静态的.它是一个候选人搜索引擎,用于将候选人放入企业的重新制定的公司.在经理可以搜索候选人的页面中,他可以通过以下方式"解析":域名(工作),城市或其他许多用户在注册时填写的内容.所有这些格式(如果它在SQL中):
[...] WHERE(domaine.domainID = 3 OR domaine.domainID = 5 OR domaine.domainID = 23)AND(cities.cityID = 4,cities.city = 32)[...]
所以我不能用普通的LINQ格式做到这一点,如:
Candidat.Domaines.Where(domain => domain.DomainID == 3 || domain.DomainID == 5 || domain.DomainID == 23);
Run Code Online (Sandbox Code Playgroud)
即使是paretheses中的运算符也是动态的("AND"或"OR")!这就是我们尝试使用Dynamic Linq的原因,因为它更灵活.
希望它更容易理解我的问题......
更新2 这是我的方法
private string BuildDomainsWhereClause() {
StringBuilder theWhere = new StringBuilder();
if (this.Domaines.NumberOfIDs > 0) {
theWhere.Append("( ");
theWhere.Append(string.Format("Domaines.Where( "));
foreach (int …Run Code Online (Sandbox Code Playgroud) 我有一个entityDao,由我的objectDaos的每个人继承.我正在使用Dynamic Linq并尝试使用一些通用查询.
我的EntityDao中的泛型方法中有以下代码:
public abstract class EntityDao<ImplementationType> where ImplementationType : Entity
{
public ImplementationType getOneByValueOfProperty(string getProperty, object getValue){
ImplementationType entity = null;
if (getProperty != null && getValue != null)
{
LCFDataContext lcfdatacontext = new LCFDataContext();
//Generic LINQ Query Here
entity = lcfdatacontext.GetTable<ImplementationType>().Where(getProperty + " =@0", getValue).FirstOrDefault();
//.Where(getProperty & "==" & CStr(getValue))
}
//lcfdatacontext.SubmitChanges()
//lcfdatacontext.Dispose()
return entity;
}
Run Code Online (Sandbox Code Playgroud)
然后我在单元测试中执行以下方法调用(我的所有objectDaos都继承entityDao):
[Test]
public void getOneByValueOfProperty()
{
Accomplishment result = accomplishmentDao.getOneByValueOfProperty
("AccomplishmentType.Name", "Publication");
Assert.IsNotNull(result);
}
Run Code Online (Sandbox Code Playgroud)
以上过程(AccomplishmentType与成就有关系)
Accomplishment result = …Run Code Online (Sandbox Code Playgroud) 我需要在运行时根据用户的输入构造一个LINQ To SQL语句,我似乎无法弄清楚如何动态构建WHERE子句.
我对以下内容没有任何问题:
string Filters = "<value>FOO</value>";
Where("FormattedMessage.Contains(@0)",Filters)
Run Code Online (Sandbox Code Playgroud)
但我真正需要的是使整个WHERE子句动态化.这样我就可以在运行时添加多个条件(粗略的想法):
foreach (Filter filter in filterlist)
{
whereclause = whereclause + "&& formattedmessage.contains(filter)";
}
Run Code Online (Sandbox Code Playgroud) 有一段时间我一直在讨论这个问题.有一些类似的情况,但解决方案不适用于我的情况.
我有一个方法以字符串格式返回过滤器查询.该方法具有不同数据类型的逻辑,设置正确的值,列名等.
string filterQuery = GetFilterQuery(params);
rows = rows.Where(filterQuery);
Run Code Online (Sandbox Code Playgroud)
我的问题是我Nullable DateTime在数据库String中有代码方面的代表.
我尝试过以下查询(String当前表示可能有误):
"BirthDate.ToString() = \"16.2.2012 22:00:00\""
Run Code Online (Sandbox Code Playgroud)
结果:类型'DateTime?'的方法 无法访问
"BirthDate.Value.ToString() = \"16.2.2012 22:00:00\""
Run Code Online (Sandbox Code Playgroud)
结果:LINQ to Entities无法识别方法'System.String ToString()'方法,并且此方法无法转换为商店表达式.
"BirthDate == null ? 1=1 : (DateTime)BirthDate.ToString() = \"16.2.2012 22:00:00\""
Run Code Online (Sandbox Code Playgroud)
结果:'.' 或'''预期
任何想法如何解决问题?
更新(添加了关于查询生成的更多源代码)
var filterQueries = query.GridFilteringOptions.filters
// remove filters that doesn't have all the required information
.Where(o => o.name != string.Empty && o.value != string.Empty && !string.IsNullOrEmpty(o.type))
// remove filters …Run Code Online (Sandbox Code Playgroud) 我正在使用动态linq库与实体框架,并希望比较日期.我已成功地扩展基于以下SO文章库这里不过似乎什么我不能做的是使该库比较只是我的实体对象的日期时间属性的日期部分,我会与正常的LINQ表达式做.
我想要做的是让动态linq像这样创建一个lambda:
// Compare just dates
p => p.CreatedDate.Value.Date == '2012/08/01'
Run Code Online (Sandbox Code Playgroud)
而不是:
// Compares date + time
p => p.CreatedDate == '2012/08/01'
Run Code Online (Sandbox Code Playgroud)
对此有任何想法将非常感激.
我正在使用 Dynamic Linq库 , 有源代码和基本文档以及Nuget版本
PM> Install-Package DynamicLINQ
我正在尝试构建一个涉及Guids的where子句
我试过用字符串"Id == @0".参数数组只是一个object[]值(Guid xxxx)
var whereClauseSB = BuildLogicalKeyWhereClause2(entity, logicalKey); //build string
var parms = BuildParamArray(entity, logicalKey); // object[]
var whereLambda = Ofsi.Bos.Core.DynamicExpression.ParseLambda<T, bool>(whereClauseSB.ToString(),parms); //parse
Run Code Online (Sandbox Code Playgroud)
DynamicExpression.ParseLambda中抛出异常
运算符'=='与操作数类型'Guid'和'Guid'不兼容
有任何想法吗?
我创建了一个通用表达式构建器,它根据条件集合构建一个谓词。我将谓词传递给存储库中的通用方法。我认为表达式生成器工作正常并创建所需的谓词,尽管实体框架生成的 SQL 脚本与我预期的不一样。我已经阅读了许多关于动态查询或 LinqKit 和表达式构建器的问题和文章,最相关的是这个评论。如果您能查看我所做的并让我知道我是否犯了任何错误,我真的很感激?
下面是 ExpressionBuilder 类的代码:
public static class ExpressionBuilder
{
private static MethodInfo containsMethod = typeof(string).GetMethod("Contains");
private static MethodInfo startsWithMethod = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
private static MethodInfo endsWithMethod = typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) });
public static Expression<Func<T, bool>> GetExpression<T>(IList<ExpressionModel> filters)
{
if (filters == null)
return null;
IList<ExpressionModel> nullFreeCollection = filters.OfType<ExpressionModel>().ToList();
if (nullFreeCollection.Count == 0)
return null;
ParameterExpression param = Expression.Parameter(typeof(T), "item");
Expression exp = null;
if (nullFreeCollection.Count == 1)
exp …Run Code Online (Sandbox Code Playgroud) 我想用这样的字符串 where 子句运行动态 linq:
query = db.Customers.Where("Categories.Any(Code == 'Retail')");
Run Code Online (Sandbox Code Playgroud)
客户实体具有类别集合
class Customer
{
public List<Category> Categories {get;set;}
...
}
class Category
{
public Guid Id {get;set;}
public string Code {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我有没有可能做这样的事情?
PS:我需要 where 子句是字符串。where 子句将在运行时生成,因此我不能使用 Linq 查询表达式。
我正在使用 Telerik DataAccess。
System.Linq.Dynamic.Core和System.Linq.Dynamic有什么区别?我目前正在使用System.Linq.Dynamic,它不包含对Select和SelectMany(以及其他扩展方法)的支持.System.Linq.Dynamic.Core是否支持这些方法?
我有一个特别报告系统;我对查询的源类型或所需字段没有编译时知识。我可以在运行时使用System.Linq.Expressions.Expression工厂方法编写表达式树,并使用反射调用 LINQ 方法,但动态 LINQ 是一个更简单的解决方案。
报告系统允许查询返回 LEFT JOIN 的结果。连接表中有字段在NOT NULL数据库中;但因为这是一个LEFT JOIN,这些字段将包含NULL某些记录。EF6 生成的表达式属于此,因为表达式投影到不可为空的值类型。
如果我在编译时 LINQ 中这样做,我会显式转换为可空类型:
enum Color { Red, Green, Blue }
// using System;
// using static System.Linq.Enumerable;
// using System.Linq;
var range = Range(0, 3).Select(x => (Color)x).AsQueryable();
var qry = range.Select(x => (Color?)x);
Run Code Online (Sandbox Code Playgroud)
动态 LINQ 支持显式转换:
// using static System.Linq.Dynamic.Core
var qry1 = range.Select("int?(it)");
Run Code Online (Sandbox Code Playgroud)
但在查询中只能引用一组特定的类型。如果我尝试Color在查询中使用:
var qry2 = range.Select("Color(it)");
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
“颜色”类型中不存在适用的方法“颜色”
如果我尝试显式转换为Color?:
var qry3 …Run Code Online (Sandbox Code Playgroud) dynamic-linq ×10
c# ×7
linq ×6
linq-to-sql ×2
.net ×1
.net-3.5 ×1
asp.net ×1
dynamic ×1
generics ×1
sql ×1
where-clause ×1