linq-to-objects

Use string as field name in LINQ

旧时模样 提交于 2019-12-04 03:02:13
Look the code below. I'd like to replace USERNAME by the field name received in the parameter field . This method must be able to make some search on several fields. Thank, public void Searching(string field, string stringToSearch) { var res = from user in _dataContext.USERs where user.USERNAME.Contains(stringToSearch) select new { Id = user.ID, Username = user.USERNAME }; } You need to forget about the anonymous type, maybe use Tuple<int,string> instead; but: how about: IQueryable<Foo> source = // YOUR SOURCE HERE // in-memory dummy example: // source = new[] { // new Foo {Id = 1, Bar = "abc"

TakeWhile, but get the element that stopped it also

我与影子孤独终老i 提交于 2019-12-04 02:59:47
问题 I'd like to use the LINQ TakeWhile function on LINQ to Objects. However, I also need to know the first element that "broke" the function, i.e. the first element where the condition was not true. Is there a single function to get all of the objects that don't match, plus the first that does? For example, given the set {1, 2, 3, 4, 5, 6, 7, 8} , mySet.MagicTakeWhile(x => x != 5); => {1, 2, 3, 4, 5} 回答1: I think you can use SkipWhile, and then take the first element. var elementThatBrokeIt =

Using LINQ to parse the numbers from a string

孤街醉人 提交于 2019-12-04 02:04:21
Is it possible to write a query where we get all those characters that could be parsed into int from any given string? For example we have a string like: "$%^DDFG 6 7 23 1" Result must be "67231" And even slight harder: Can we get only first three numbers? This will give you your string string result = new String("y0urstr1ngW1thNumb3rs". Where(x => Char.IsDigit(x)).ToArray()); And for the first 3 chars use .Take(3) before ToArray() The following should work. var myString = "$%^DDFG 6 7 23 1"; //note that this is still an IEnumerable object and will need // conversion to int, or whatever type

How to group ranges in LINQ

孤人 提交于 2019-12-03 21:46:12
Here is the data that I would like to group. Start End 2 4 26 30 5 9 20 24 18 19 Because I have 18 - 19 and 20 - 24. I would add these two together as 18 - 24. In this case the rule is (a, b) => b.start - a.end = 1 and the result would be Start End 18 24 2 9 26 30 EDIT added last result row per comments below. So we'll start with a helper method called GroupWhile . It will be provided with a predicate accepting two items from the sequence, the previous and the current. If that predicate returns true, the current item goes into the same group as the previous item. If not, it starts a new group.

Handling temporary calculation in Linq

a 夏天 提交于 2019-12-03 16:30:27
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 I will get 219978 back. I solved it using for (long l = 100000; l < 999999; l++) { var num = l.ToString(); for (int i = 3; i < 9; i++) { var mul = l * i; string str = mul.ToString(); char[] splitDigits = str.ToCharArray(); string reversedDigit = new string(splitDigits.Reverse().ToArray()); if (reversedDigit

Get a list of distinct items and their count

冷暖自知 提交于 2019-12-03 16:21:11
问题 I have an object, that has many properties but the only two to worry about are: myobject.ID which is an int myobject.Names which is a HashSet Then I have a List of those objects that looks something similar to this: List<myobject> I use Linq to get a some of the data into a repeater, but I'm not sure how to get the list of Names and how often they show up. Want to use Linq to avoid having to loop through the data. As my tags should show, this is an ASP.NET solution using C#. Some

In statement for LINQ to objects

半腔热情 提交于 2019-12-03 15:15:38
Is there an equivalent of a SQL IN statement in LINQ to objects? Yes - Contains . var desiredNames = new[] { "Jon", "Marc" }; var people = new[] { new { FirstName="Jon", Surname="Skeet" }, new { FirstName="Marc", Surname="Gravell" }, new { FirstName="Jeff", Surname="Atwood" } }; var matches = people.Where(person => desiredNames.Contains(person.FirstName)); foreach (var person in matches) { Console.WriteLine(person); } (In LINQ to SQL this ends up as an "IN" query.) Note that in LINQ to Objects the above isn't really very efficient. You'd be better off with a join: var matches = from person in

IQueryable Lambda Projection Syntax

跟風遠走 提交于 2019-12-03 14:54:09
I have an IQueryable whose Entity Framework 4 objects I would like to project to their DTO equivalents. One such object 'Person' is an EF4 class, and the corresponding POCO PersonP is a class I've defined. I am using Automapper to map between them. However, when I try the following code: IQueryable<Person> originalModel = _repo.QueryAll(); IQueryable<PersonP> projection = originalModel.Select(e => Mapper.Map<Person, PersonP>(e)); The projection generates this error at runtime: LINQ to Entities does not recognize the method 'TestSite.Models.PersonP Map[Person,PersonP](TestSite.DataLayer.Model

LINQ to Entities / LINQ to SQL: switching from server (queryable) to client (enumerable) in the middle of a query comprehension?

谁都会走 提交于 2019-12-03 13:02:46
In many cases, I want to do some filtering (and sometimes projection) on the server side and then switch to client-side for operations that the LINQ provider doesn't natively support. The naive approach (which is basically what I do now) is to just break it up into multiple queries, similar to: var fromServer = from t in context.Table where t.Col1 = 123 where t.Col2 = "blah" select t; var clientSide = from t in fromServer.AsEnumerable() where t.Col3.Split('/').Last() == "whatever" select t.Col4; However, there are many times where this is more code/trouble than it's really worth. I'd really

Filtering lists using LINQ

*爱你&永不变心* 提交于 2019-12-03 11:06:21
问题 I've got a list of People that are returned from an external app and I'm creating an exclusion list in my local app to give me the option of manually removing people from the list. I have a composite key which I have created that is common to both and I want to find an efficient way of removing people from my List using my List e.g class Person { prop string compositeKey { get; set; } } class Exclusions { prop string compositeKey { get; set; } } List<Person> people = GetFromDB; List