Concat Two IQueryables with Anonymous Types?

I struggled with this a bit, and it starts to look like it might be impossible.

I want Concat() two IQueryable , and then execute the result as a single query. I tried something like this:

 var query = from x in ... select new { A = ... B = ... C = ... }; var query2 = from y in ... select new { A = ... B = ... C = ... }; var query3 = query.Concat(query2); 

However, the last line gives me the following error:

'System.Linq.IQueryable' does not contain a definition for "Concat" and a better extension overload method. "System.Linq.ParallelEnumerable.Concat (System.Linq.ParallelQuery, System.Collections.Generic.IEnumerable)" has some invalid arguments

It seems to be expecting IEnumerable for an argument. Is there any way around this?

It looks like I could resolve both IEnumerable requests and then Concat() them. But it would be more efficient to create a single request, and it seems that this should be possible.

+6
source share
4 answers

As you said earlier in the comments, it seems that two queries return different objects:

Request 1 (according to the comment):

 f__AnonymousTypee<Leo.Domain.FileItem,Leo.Domain.Employ‌​ee,int,string,string> 

Query2 -

 f__AnonymousTypee<Leo.Domain.FileItem,L‌​eo.Domain.Employee,int?,string,string> 

This is why Concat gives you an error message reporting invalid arguments.

+5
source

The defined IDE request and request2 have different types, whereas the IEnumerable <TSource> Concat <TSource> () extension method assumes two of the same type (IEnumerable <TSource>). Three TSource must be the same.

 string[] strA = {"123", "234", "345"}; int[] intA = { 1, 2, 3 }; var query = from s in strA select s; var query2 = from i in strA // intA select i; var query3 = query.Concat(query2); 

Uncomment "// intA" in VS and you will see the difference.

+1
source

Anonymous objects will be equivalent types to other anonymous objects with the same property names and types that are specified in exactly the same order.

Assuming that both query and query2 from the same context, you should be able to combine these two if they are queries of equivalent types.

Your comment indicates that they are not of the same type.

query returns objects of the type Anon<FileItem, Employee, int, string, string>
query2 returns objects of the type Anon<FileItem, Employee, int?, string, string> .

You cannot combine these two because they are of different types. You need to make sure that both queries return objects of the same type.

 var query = from x in ... select new { A = (FileItem)... B = (Employee)... C = (int)... ... }; var query2 = from y in ... select new { A = (FileItem)... B = (Employee)... C = (int)... ... }; 
+1
source

Are you missing a namespace? I usually put my .NET Project Properties in the target .net 4.0 for vs 2010. I do not use the .net 4.0 Client Profile.

Please ensure that types A, B, and C are the same as in anonymous request types. Also, the order of A, B, and C must also match in both queries.

The following example works like a charm.

 namespace Test { using System; using System.Collections.Generic; using System.Linq; internal class Employee { public string Name { get; set; } public int Age { get; set; } public double Salary { get; set; } public string Address { get; set; } } internal class Program { private static List<Employee> employees = new List<Employee>(); private static void BuildList() { employees.AddRange( new Employee[] { new Employee() {Name = "Tom", Age = 22, Address = "sample1", Salary = 10000}, new Employee() {Name = "Mithun", Age = 27, Address = "sample1", Salary = 20000}, new Employee() {Name = "Jasubhai", Age = 24, Address = "sample1", Salary = 12000}, new Employee() {Name = "Vinod", Age = 34, Address = "sample1", Salary = 30000}, new Employee() {Name = "Iqbal", Age = 52, Address = "sample1", Salary = 50000}, new Employee() {Name = "Gurpreet", Age = 22, Address = "sample1", Salary = 10000}, } ); } private static void Main(string[] args) { BuildList(); var query = from employee in employees where employee.Age < 27 select new { A = employee.Name, B = employee.Age, C = employee.Salary }; var query2 = from employee in employees where employee.Age > 27 select new { A = employee.Name, B = employee.Age, C = employee.Salary }; var result = query.Concat(query2); foreach (dynamic item in result.ToArray()) { Console.WriteLine("Name = {0}, Age = {1}, Salary = {2}", item.A, item.B, item.C); } } } } 
0
source

Source: https://habr.com/ru/post/971575/


All Articles