linq

Compare two arrays using LINQ

删除回忆录丶 提交于 2021-01-27 16:27:12
问题 For example, I have two arrays: string[] arrayOne = {"One", "Two", "Three", "Three", "Three"}; string[] arrayTwo = {"One", "Two", "Three"}; var result = arrayOne.Except(arrayTwo); foreach (string s in result) Console.WriteLine(s); I want Items from arrayOne which are not there in arrayTwo . So here I need result as: Three Three but I am getting no results as its treating "Three" as common and not checking the other two items("Three", "Three"). I dont want to end up writing a huge method to

Difference between LINQ FirstOrDefault vs. Where(…).FirstOrDefault?

我的梦境 提交于 2021-01-27 16:06:00
问题 What difference between FirstOrDefault(someField => someField.Name.Equals(settings.Text)) and Where(someField => someField.Name.Equals(settings.Text)).FirstOrDefault() ? As far as I understand in both cases Linq will run till the first occurence that suits the condition. 回答1: If we are talking about Linq to Objects, then there is one notable difference. Second statement Where(someField => someField.Name.Equals(settings.Text)).FirstOrDefault() Will create WhereEnumerableIterator internally,

Difference between LINQ FirstOrDefault vs. Where(…).FirstOrDefault?

懵懂的女人 提交于 2021-01-27 16:04:31
问题 What difference between FirstOrDefault(someField => someField.Name.Equals(settings.Text)) and Where(someField => someField.Name.Equals(settings.Text)).FirstOrDefault() ? As far as I understand in both cases Linq will run till the first occurence that suits the condition. 回答1: If we are talking about Linq to Objects, then there is one notable difference. Second statement Where(someField => someField.Name.Equals(settings.Text)).FirstOrDefault() Will create WhereEnumerableIterator internally,

Partial Distinct Linq

非 Y 不嫁゛ 提交于 2021-01-27 15:59:53
问题 I have a list of objects. These objects have a property e.g. "Value". var lst = new List<TestNode>(); var c1 = new TestNode() { Value = "A", }; lst.Add(c1); var c2 = new TestNode() { Value = "A", }; lst.Add(c2); var c3 = new TestNode() { Value = "B", }; lst.Add(c3); var c4 = new TestNode() { Value = "B", }; lst.Add(c4); I would like to say something like: lst.PartialDistinct(x => x.Value == "A") This should only be distinct by predicate and when printing the "Value"s of the resulting

Difference between LINQ FirstOrDefault vs. Where(…).FirstOrDefault?

a 夏天 提交于 2021-01-27 15:51:55
问题 What difference between FirstOrDefault(someField => someField.Name.Equals(settings.Text)) and Where(someField => someField.Name.Equals(settings.Text)).FirstOrDefault() ? As far as I understand in both cases Linq will run till the first occurence that suits the condition. 回答1: If we are talking about Linq to Objects, then there is one notable difference. Second statement Where(someField => someField.Name.Equals(settings.Text)).FirstOrDefault() Will create WhereEnumerableIterator internally,

How to cast IQueryable<T> to DbSet<T>?

会有一股神秘感。 提交于 2021-01-27 13:02:41
问题 public DbSet<Item> Items { get { return dbContext.Item.Where(x => x.Id == id).Select(x=>x) } } The above code causes a compilation error: Cannot implicitly convert type 'System.Linq.IQueryable to ... DbSet. An explicit conversion exists (are you missing a cast?) After adding the explicit cast: public DbSet<Item> Items { get { return (DbSet<Item>)(dbContext.Item.Where(x => x.Id == id).Select(x => x)) } } a runtime error happens: Additional information: Unable to cast object of type 'Microsoft

How to cast IQueryable<T> to DbSet<T>?

北慕城南 提交于 2021-01-27 12:59:57
问题 public DbSet<Item> Items { get { return dbContext.Item.Where(x => x.Id == id).Select(x=>x) } } The above code causes a compilation error: Cannot implicitly convert type 'System.Linq.IQueryable to ... DbSet. An explicit conversion exists (are you missing a cast?) After adding the explicit cast: public DbSet<Item> Items { get { return (DbSet<Item>)(dbContext.Item.Where(x => x.Id == id).Select(x => x)) } } a runtime error happens: Additional information: Unable to cast object of type 'Microsoft

EFCore 3.1 - Exists query via Any; Query cannot be translated

孤街醉人 提交于 2021-01-27 11:20:38
问题 We're using EFCore 3.1 and trying to build a query using Exists by means of .Any() which spans 2 properties. var selectionCriteria = someHugeList.Select(sh => new { sh.Id, sh.StatusCode }).ToList() var resultsQry = _myContext.SomeClass .Include(sc => sc.DetailRecords) .Where(sc => selectionCriteria.Any(crit => crit.Id == sc.Id && crit.StatusCode == sc.StatusCode)); var results = await resultsQry.ToListAsync() When running this query (even with a small amount (5 items) of selection criteria

EFCore 3.1 - Exists query via Any; Query cannot be translated

丶灬走出姿态 提交于 2021-01-27 11:20:07
问题 We're using EFCore 3.1 and trying to build a query using Exists by means of .Any() which spans 2 properties. var selectionCriteria = someHugeList.Select(sh => new { sh.Id, sh.StatusCode }).ToList() var resultsQry = _myContext.SomeClass .Include(sc => sc.DetailRecords) .Where(sc => selectionCriteria.Any(crit => crit.Id == sc.Id && crit.StatusCode == sc.StatusCode)); var results = await resultsQry.ToListAsync() When running this query (even with a small amount (5 items) of selection criteria

EFCore 3.1 - Exists query via Any; Query cannot be translated

偶尔善良 提交于 2021-01-27 11:17:09
问题 We're using EFCore 3.1 and trying to build a query using Exists by means of .Any() which spans 2 properties. var selectionCriteria = someHugeList.Select(sh => new { sh.Id, sh.StatusCode }).ToList() var resultsQry = _myContext.SomeClass .Include(sc => sc.DetailRecords) .Where(sc => selectionCriteria.Any(crit => crit.Id == sc.Id && crit.StatusCode == sc.StatusCode)); var results = await resultsQry.ToListAsync() When running this query (even with a small amount (5 items) of selection criteria