Tag: linq

比较LINQ to SQL中的两个日期

我有一个名为meeting的数据库。 会议日期使用以下格式存储在此表中: May 2nd 2011 (例如)格式化为5/2/2011 。 我的要求是在两个日期(例如,2011年4月25日和2011年5月2日)之间召开会议,并编写一个查询,将日期与这两个日期进行比较。 是否比较4/25/2011 <4/26/2011? 比较是如何进行的? 我正在使用SQL Server 2008和LINQ to SQL查询。

如何在Linq中获得以下输出

如何模拟此Sql查询: Select A.FID, A.SLNO, A.Date as FromDate, A1.Date ToDate From #Test A Inner Join #Test A1 On A.FID = A1.FID And A1.SLNO = A.SLNO + 1 这使: 要使用此查询提供相同的输出: var results2 = from table1 in dtSplitDates.AsEnumerable() join table2 in dtSplitDates.AsEnumerable() on new { FID = table1.Field(“FID”), SLNO = table1.Field(“SLNO”)} equals new { FID = table2.Field(“FID”), SLNO […]

有没有办法在linq查询中参数化方法?

在我使用Linq to SQL的应用程序中,用户可以搜索文本。 可以在搜索表达式的开头和/或结尾使用星号(*)。 代码现在是这样的: var search = SearchTextBox.Text.Trim(); bool filterStartsWith = false, filterEndsWith = false; if (!string.IsNullOrEmpty(search)) { filterStartsWith = search.EndsWith(“*”); filterEndsWith = search.StartsWith(“*”); if (filterStartsWith) search = search.Substring(0, search.Length – 1); if (filterEndsWith) search = search.Substring(1); if (filterStartsWith) { if (filterEndsWith) { query = query.Where(item => item.Omschrijving.Contains(search)); } else { query = query.Where(item […]

如何调用Dictionary .TryGetValue()其中K:Predicate ,V:enum

我有Dictionary<Predicate, SomeEnum> : var dic = new Dictionary<Predicate, SomeEnum> { { (d) => d d > 90, SomeEnum.Bar } }; 我想像这样打电话给TryGetValue(K, out V) : dic.TryGetValue(99) 并收到 SomeStruct.Bar 但TryGetValue()第一个参数是Predicate ,而不仅仅是T 我怎么能做我想做的事? 我发现只有一个肮脏的解决方法: var kpv = dic.FirstOrDefault(p => p.Key(99)); if (kpv.Key != null) var result = kpv.Value; 还有其他方法吗? 或者如何正确实现我的想法? – 声明一个键不是一个常量,而是一个段。

如何在LINQ lambda扩展方法中插入方法

我必须在LINQ lambda中放置一个方法。 我有以下代码: string productName = “Some product”; searchShoppingCart = shoppingCart.Where(m => productName.Equals(_ProductRepository.GetProductName(m.Id))); 基本上,此代码用于选择包含productName所有shoppingCart实例。 方法string GetProductName(int shoppingCartId)是_ProductRepository中的一个方法,它返回一个产品的名称。 如何实现此方法及其逻辑无关紧要。 问题是LINQ抛出exception 。 我知道抛出此exception的原因,我只是想知道一个解决方法。 我试过了 var productContained = shoppingCart.Select(sc => new { scId = sc.Id, productName = _ProductRepository.GetProductName(sc.Id) }); searchShoppingCart = shoppingCart.Where(sc => sc.Id.Equals(productContained.Where(pc => pc.productName.Equals(productName)) .Select(pc => pc.Id))); 但它给了我同样的例外。 还有其他解决方法吗? UPDATE 抛出的exception是 LINQ to Entities无法识别方法’System.String GetProductName(Int32)’方法,并且此方法无法转换为商店表达式。

在运行时动态生成谓词

不确定这是否完全可能; 我发现了一些关于表达式和谓词构建器的东西,但到目前为止,没有任何东西可以让你在不事先知道它们的情况下运行任意查询。 基本上,我有一个来自大型SQL数据库的对象集合,我正在构建一个网页(ASP.NET MVC 4),允许用户显示和过滤这些对象。 用户将要输入的查询的复杂程度各不相同。 让他们输入这些查询的最简单和最简单的方法就像Visual Studio TFS插件允许您搜索工作项的方式:条件表,您可以继续添加行。 为连接条件选择“和”或“或”,然后选择一个字段,输入一个值,并选择是否需要与之匹配或不匹配的事物: 1. show items where [Field] [is|is not] [value] 2. [and|or] [Field] [is|is not] [value] 3. [and|or] [Field] [is|is not] [value] etc… 什么是最简单的方法将它变成LINQ-ish我可以坚持一个.ToList()的结尾? 到目前为止,我提出的唯一解决方案是涉及一个相当大且丑陋的开关块,其中的情况与各种字段匹配并且粘贴在.Where() ,但允许用户选择“或”作为条件然后我最终做了这样的事情: 条件是AND: 使用大开关匹配该字段 query = query.Where(ThisField == value); 当你遇到OR的条件时: 将当前结果附加到临时列表 来自完整未过滤列表的新查询 使用大开关匹配该字段 query = fullList.Where(ThisField == value); 继续像以前一样 当您用完条件时,将当前结果集追加到您一直使用的临时列表中,并返回该列表。 这看起来不像我想的那么优雅。

LINQ关键字与方法

我可以: var something = things.Where(thing => thing.stuff == yup); var somethingelse = something.Select(thing => thing.otherstuff); 要么 var something = from thing in things where thing.stuff == yup select thing; var somethingelse = from thing in something select thing.otherstuff; 显然,如果这是真实的世界,关键字版本的做法有好处: var somethingelse = from thing in something where thing.stuff == yup select thing.otherstuff; 但当然你可以说你可以做到: var somethingelse […]

如何使用Expression.Call在对方法的调用中嵌套对方法的调用

我试图在调用包含它之前将DateTime转换为String。 尽管我努力将一个表达式的结果放到另一个表达式中,但我却悲惨地失败了。 代码来自这个问题的最高答案jqgrid与asp.net webmethod和json使用排序,分页,搜索和LINQ – 但需要动态运算符。 假设我从问题中得到以下方法作为StringExtension: public static class StringExtensions { public static MemberExpression ToMemberExpression(this string source, ParameterExpression p) { if (p == null) throw new ArgumentNullException(“p”); string[] properties = source.Split(‘.’); Expression expression = p; Type type = p.Type; foreach (var prop in properties) { var property = type.GetProperty(prop); if (property == null) throw […]

是否可以使用LINQ从存储过程返回返回值和结果集

寻找关于获取返回值和结果集的最干净方法的建议(不将引用的参数传递给存储的proc)。 我的存储过程具有返回值以显示错误等,它们以select语句结束以获取信息。 使用常规命令,我将创建一个输出参数,该参数将保存返回值。 我无法更改db中的存储过程,因此除了返回值之外,我无法传递不同的输出参数。 想法?

不支持使用类型1的输入和类型2的检查的LINQ抛出TypeAs表达式

这一行给了我‘TypeAs’表达式,其输入类型为User,不支持类型为SoftDeleteEntity的检查 : var test = this.partiallyFiltered.Where(additionalFilter).ToList(); 这就是那里的东西。 partiallyFiltered的类型为IQueriable 。 附加filter的类型为Expression<Func> ,并且传递的实际表达式(根据调试器)是!((x as SoftDeleteEntity).IsDeleted) 。 用户inheritanceSoftDeleteEntity。 我不认为我错过任何相关信息,但如果我告诉我,我会详细说明。 我明白必须有一个隐含的演员在某个地方不起作用,但我找不到它。 编辑:这里是表达式的声明,所有实体都是IEntity。 public static Expression<Func> DefaultFilter() where TEntity : IEntity { if (typeof(SoftDeleteEntity).IsAssignableFrom(typeof(TEntity))) return x => !(x as SoftDeleteEntity).IsDeleted; else return x => true; }