Tag: linq

无法将类型’Program.Data.View’隐式转换为System.linq.iqueryable 。

目标/问题:我尝试使用First或FirstOrDefault仅从数据库返回1个结果。 我收到以下错误: 无法将类型’Program.Data.view’隐式转换为System.Linq.Iqueryable存在显式转换(您是否缺少转换) 我尝试过的内容:在查看了文档和许多SO文章之后,我尝试了不同的投射方式,包括下面的代码。 诸如无法将类型’System.Linq.IQueryable 隐式转换为的文章。 存在显式转换(您是否错过了演员?) 。 大多数文章都是从System.Linq.IQueryable 转到其他方面,而不是这个方向。 无论哪种方式铸造应该相对容易,我缺少什么?: IQueryable someVariable = db.view.First(m => m.ID == Search_ID); 我的方法签名是: public IQueryable GetDataFromQuery()

LINQ组一种类型的项目

我有一个包含各种派生类的List。 我可能有这样的事情: List list = new List() { new Class1(), new Class2(1), new Class3(), new Class2(2), new Class4() }; 我试图使用LINQ对列表进行半排序,以便保持自然顺序除了Class2。 所有Class2实例应在第一个Class2出现的位置组合在一起。 这是输出应该是什么样的: List list = new List() { new Class1(), new Class2(1), new Class2(2), new Class3(), new Class4() }; 我不能为我的生活弄清楚如何做到这一点……

如何在join linq语法中比较null

加入EF 4 C #Face问题。 sql语法 Select a.Code, b.Name from DepartmentMaster a Join DepartmentDetail b on isnull( a.ID,0) =isnull( b.ID,0) 注意: a.ID,b.ID都可以为空 想要使用Linq语法输出上面的语法.Bellow语法对我不起作用 Var r=from a in DepartmentMaster Join b in DepartmentDetail on a.ID equals b.ID Select a.Code,b.Name 需要帮助在linq ef中编写sql语法isnull()可比较的过程。 如有任何疑问请咨询。

使用linq检查另一个数组中是否存在来自一个数组的值

我有一个列表,其中myclass定义了一些属性 List myClassList = new List(); myClassList.Add(new MyClass() { Id = 1, Name = “My first test”, Key = “First” }); myClassList.Add(new MyClass() { Id = 2, Name = “My second test”, Key = “Second” }); 然后我有一个路径,即c:\ my folders \ company name \ My First Test,我使用regex闯入字符串数组,即myArrayPath 我想找到myClassList的任何基于给定属性的元素,即Name可以在myArrayPath的任何元素中找到,理想情况下我想返回Key,但如果我返回匹配myArrayPath中的一个元素的对象,这将同样好。 有没有办法使用linq和/或lambda表达式来实现这一点。 谢谢。

基于谓词拆分LINQ查询

我想要一个将IEnumerable分解为谓词的方法,通过它们相对于谓词的索引将项目分组在一起。 例如,它可以在满足x => MyRegex.Match(x).Success项目中拆分List ,其中“中间”项目将这些匹配组合在一起。 它的签名可能看起来像一些线 public static IEnumerable<IEnumerable> Split( this IEnumerable source, Func predicate, int bool count ) ,可能还有一个包含所有分隔符的输出的额外元素。 有没有比foreach循环更有效和/或更紧凑的方法来实现它? 我觉得应该可以用LINQ方法实现,但我不能指责它。 例: string[] arr = {“One”, “Two”, “Three”, “Nine”, “Four”, “Seven”, “Five”}; arr.Split(x => x.EndsWith(“e”)); 以下任何一种都可以: IEnumerable {{}, {“Two”}, {}, {“Four”, “Seven”}, {}} IEnumerable {{“Two”}, {“Four”, “Seven”}} 存储匹配的可选元素是{“One”, “Three”, “Nine”, “Five”} 。

不能在MongoDb C#上使用Linq和嵌套类List

我有以下课程: public class Company { [BsonId] public string dealerId = null; public List dealers = new List(); } public class Dealer { public string dId = null; public int dIndex = -1; public List stores = new List(); } public class AutoStore { public string type = null; public Dictionary data = new Dictionary(); } […]

如何使用linq过滤和求和

什么是最好的LINQ方法,没有性能损失? 我们怎么能用LINQ迭代列表一次呢? class Employee { string name …; string age …..; int empType …; int income …; } List myList …; List toList…; int totalIncomeOfToList = 0; foreach(Employee emp in myList) { if(emp.empType == 1) { toList.Add(emp); totalIncomeOfToList += emp.income; } }

使用IQueryable创建动态查询

我正在尝试迭代字符串数组并动态创建IQueryable查询。 它很简单,但这里是我被困的地方 var query = context.QuestionsMetaDatas.AsQueryable(); var keywords=new List(){ “Test1″,”Test2” }; foreach(var key in keywords) { query=query.Where(a=>a.Text.Contains(key)); } 现在的问题是,当查询生成时,它编译为 select * from QuestionsMetaDatas where Text Like “Test1” AND Text Like “Test2” 而不是AND我希望查询生成OR …现在我该如何实现?

如何通过契约定义IEnumerable行为?

考虑这两个返回IEnumerable的方法: private IEnumerable GetYieldResult(int qtResult) { for (int i = 0; i < qtResult; i++) { count++; yield return new MyClass() { Id = i+1 }; } } private IEnumerable GetNonYieldResult(int qtResult) { var result = new List(); for (int i = 0; i < qtResult; i++) { count++; result.Add(new MyClass() { Id = i […]

将算法从C#转换为VB.NET失败

我正在尝试将以下算法从C#转换为VB.NET,而我所拥有的VB.NET并没有产生与我的C#算法相同的结果,有人可以告诉我在转换中出错了吗? public static IEnumerable Combinations(this IEnumerable elements, int k) { List result = new List(); // single combination if (k == 0) { result.Add(new T[0]); } else { int current = 1; foreach (T element in elements) { //combine each element with k-1 combinations of subsequent elements result.AddRange(elements .Skip(current++) .Combinations(k – 1) .Select(combination => (new […]