2
投票
3答案
237 次观看

使用LINQ查找通用前缀?

我有两个序列: IEnumerable<string> x = new[] { "a", "b", "c" }; IEnumerable<string> y = new[] { "a", "b", "d", "e" }; 我想找到这两个序列的通用前缀(即"a", "b")。在LINQ中有一种简洁的方法吗? 请记住,这些并不是真正的IEnumerable<string>。他们是IEnumerable<PathComponent>,我在这里实现了IEqualityComparer<PathComponent>。

5
投票
4答案
3501 次观看

LINQ to Object和LINQ to SQL查询之间的差异

一段时间以来,我一直在使用LINQ查询我的POCO对象,但是我还没有尝试过LINQ to SQL。我假设LINQ to SQL查询以某种方式转换为等效的SQL查询,并且鉴于此,我想知道这是否影响LINQ to SQL查询的编写方式或应该如何编写。 LINQ to Objects和LINQ to SQL之间是否有任何显着差异会影响我应该如何编写查询?

22
投票
3答案
25382 次观看

使用Linq从另一个对象的列表创建一个对象类型的列表

如果我有A型和B型课程: public class A { public int TotalCount; public string Title; } public class B { public int Count; public string Title; } 我有一个实例A实例列表,使用Linq创建和填充B型列表的最有效方法是什么?

3
投票
3答案
2084 次观看

动态LINQ查询

是否可以在运行时创建Linq查询。 使用可以转换为Linq查询的xml规则。

0
投票
1答案
1552 次观看

需要帮助将Linq.Expression创建为Enumerable.GroupBy

我正在尝试生成一个表达式树,该树最终会调用Enumerable类型的一系列GroupBy方法。 以简化形式,我正在尝试如下操作: IEnumerable<Data> list = new List<Data>{new Data{Name = "A", Age=10}, new Data{Name = "A", Age=12}, new Data{Name = "B", Age=20}, new Data{Name="C", Age=15}}; Expression data = Expression.Parameter(typeof(IE...

24
投票
8答案
37070 次观看

使用LINQ to Objects在一个集合中查找与另一个不匹配的项目

我想找到一个集合中与另一个集合不匹配的所有项目。但是,集合的类型不同。我想编写一个lambda表达式来指定相等性。 我要执行的操作的 LINQPad 示例: void Main() { var employees = new[] { new Employee { Id = 20, Name = "Bob" }, new Employee { Id = 10, Name = "Bill" }, new Employee { Id = 30, Name = "Frank" } }; var manager...

8
投票
3答案
7126 次观看

如何使用Linq确定此字符串是否以值EndsWith(来自集合)?

我正在尝试查找字符串值EndsWith是否为另一个字符串。此“其他字符串”是集合中的值。我正在尝试将其作为字符串的扩展方法。 例如 var collection = string[] { "ny", "er", "ty" }; "Johnny".EndsWith(collection); // returns true. "Fred".EndsWith(collection); // returns false.

0
投票
1答案
1106 次观看

Linq连续日期需要

主要问题:仅当连续且仅当它们与某些属性匹配时,才如何按日期对元素进行分组? 详细信息: 给出这种类型的对象: public class MyObj { public DateTime Date { get; set; } public string ConditionalValue1 { get; set; } public int ConditionalValue2 { get; set; } public int OtherValue { get; set; } //for test purposes only, i would l...

2
投票
1答案
146 次观看

有没有一种方法可以分析特定的Linq-to-objects查询将如何执行?

过去,我曾将Linq编写为性能不佳的SQL查询。使用SQL事件探查器(或类似工具),我可以通过在数据库中截取查询来查看如何将查询转换为SQL。 是否可以对仅对对象进行操作的Linq查询执行此操作? 作为示例,请考虑以下关于有向图边列表的Linq查询: var outEdges = from e in Edges where e.StartNode.Equals(currentNode) && !(from d in deadEdges select d.StartNode).Contains(e.En...

0
投票
1答案
68 次观看

LINQ实现查询?

我正在根据每个内部的两个不同条件设置两个值。是否可以在LINQ中实现以下内容? foreach(Customer cust in Customers) { If(Condition 1) condtion1Var =true; If(Condition2 ) condition2Var =true; }

0
投票
3答案
108 次观看

是否存在用于显示Linq到对象查询的某种查询计划的应用程序?

我正在寻找一个显示linq表达式的应用程序,特别是关于在查询中对同一列表的多次访问的使用。 更好的是,该工具会判断linq查询是否正确。

317
投票
11答案
292095 次观看

使用linq删除列表中的重复项

我有一个Items和properties (Id, Name, Code, Price)的班级。 Items列表中填充有重复项。 例如: 1 Item1 IT00001 $100 2 Item2 IT00002 $200 3 Item3 IT00003 $150 1 Item1 IT00001 $100 3 Item3 IT00003 $150 如何使用linq删除...

1
投票
4答案
1588 次观看

LINQ到对象vs每个对象-执行时间的差异

每个循环我都有这个 foreach (Account acct in acctTest) { if (acct.AccountId == acctId) { foreach (Customer cust in acct.CustomerColl) { if (cust.CustomerId == custId) { ...

1
投票
5答案
657 次观看

Linq Arithemetic Operator combinations

When attemptiing to solve the below assignment : Using the Arithmetic operators ( +,-,*,/) rearrange four fives to equal the number 1 to 10. Example : 5/5+5-5 =1 ,5/5+5/5=2 I tried in C# without using Linq (I don't know how to proceed further) public void GetDetails() { char[]...

1
投票
2答案
719 次观看

Solving brain teaser using linq

At the very outset let me express my sincere thanks to Marc Gravel,Dahlbyk and the rest for helping me to apply linq practically. The following are few questions which I have faced in an interview to solve applying Linq. As I am not familiar with Linq I solved it without using Linq. I apprec...

4
投票
2答案
974 次观看

Handling temporary calculation in Linq

When solving an interview question Question A six digit number need to be found in such a manner when it is multiplied by an integer between 2 and 9 gives the original six digit number when its digits are reversed. Example: Suppose I multiply 219978 * 4 i get 879912 ,when reverse 879912...

2
投票
1答案
208 次观看

Selecting combination using linq

This is also one of the interview question i have faced recently. Description : The task is $ 100(Please consider some currency) will be given to me.I need to purchase three items itemA,itemB,itemC.The cost of (I am not sure 0.25$ or 0.75 $ are meaningful,so think it as other currency) itemA=0....

2
投票
3答案
230 次观看

Linq Compound selection

How can i combine two arrays in to a single array during compound selection ( without using Union ) ( The question was asked at interview). var num1 = new int[] { 12, 3, 4, 5 }; var num2 = new int[] { 1, 33, 6, 10 }; I tried as var pairs = from a in num1 from b in num2 select new...

0
投票
4答案
154 次观看

How the transformation is possible?

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven","eight", "nine" }; var textNums = from n in numbers select strings[n]; Console.WriteLine("Number strings:...

3
投票
3答案
1199 次观看

How to get the Point with minimal X from an array of Points without using OrderBy?

Imagine I have var points = new Point[] { new Point(1, 2), new Point(2, 3) }; To get the point with the minimum X I could: var result = points.OrderBy(point => point.X).First(); But for large arrays, I don't think this is the faster option. There is a faster alternative?

4
投票
4答案
1771 次观看

Appropriate use of keyword "new" in LINQ

Please consider the following code : string[] words = { "Too", "much", "of", "anything", "is" ,"good","for","nothing"}; var groups =from word in words orderby word ascending group word by word.Length into lengthGroups orderby lengthGroups.Key descending ...

4
投票
3答案
3401 次观看

Implementing Row-Level Security – (SPs vs LINQ to Objects)

I believe this is more a question about best practices and design than anything else. I tried searching for similar queries regarding this but couldn’t find any. I actually found the Row Level Security with Entity Framework but I believe the context here is a bit different. I will try to explain...

11
投票
1答案
10284 次观看

Select distinct values in all nested collections using LINQ to objects?

Given the following code setup: public class Foo { List<string> MyStrings { get; set; } } List<Foo> foos = GetListOfFoosFromSomewhere(); How do I get a list of all of the distinct strings in MyStrings across all of the Foo instances using LINQ? I feel like this should be easy, bu...

0
投票
1答案
1041 次观看

LINQ Gernic List VB.NET

I am trying to create a domain model using a product class entity an Abstract repository and a fake repository with sample data. I have created the following product class Namespace Entities Public Class Product Dim _ProductID As Integer Dim _Name As String Dim _Description As Strin...

0
投票
2答案
3981 次观看

How to get All select keys in Linq Group By Clause

I am trying to get the rest of the select keys in this linq query but the intellisense is giving me an error var query2 = from row2 in query1 group row2 by row2.questionid into g where g.Count() > 0 select new ...

5
投票
3答案
19112 次观看

How to use a Distinct on a column using Linq

Here is my code: var query = from row1 in table.AsEnumerable() let time = row1.Field<DateTime>("time") let uri = row1.Field<string>("cs-uri-stem") let ip = row1.Field<string>("c-ip") ...

44
投票
7答案
9133 次观看

LINQ identity function?

Just a little niggle about LINQ syntax. I'm flattening an IEnumerable<IEnumerable<T>> with SelectMany(x => x). My problem is with the lambda expression x => x. It looks a bit ugly. Is there some static 'identity function' object that I can use instead of x => x? Something li...

3
投票
1答案
450 次观看

Confused about how to implement extension methods in LINQ to Objects

Fairly new to LINQ and trying to wrap my head around extension methods. What I'm attempting to do: 1) Have a table with three columns, col1 (string), col2 (double), col3 (DateTime) table1.Rows.Add("string 1", 1, new DateTime(2009, 01, 01)); table1.Rows.Add("string 1", 2, new Dat...

0
投票
3答案
162 次观看

Linq-to-objects processing time doubles per x iterations

I have a list of entites that contains ~137000 records that I loop through I then need to linq to a list of Tuple of additional params this contains ~ 150000 Why does it keep taking longer, the more iterations it does? Here is from the stopwatch Found: 136770 items that match the criteria. 1...

0
投票
5答案
182 次观看

Can a single LINQ Query Expression be framed in this scenario?

I am facing a scenario where I have to filter a single object based on many objects. For sake of example, I have a Grocery object which comprises of both Fruit and Vegetable properties. Then I have the individual Fruit and Vegetable objects. My objective is this: var groceryList = from grocer...