2
List<MyObject> objects = await item.tables.ToAsyncEnumerable()
                               .Where(p => p.field1 == value)
                               .Select(p => new MyObject(p.field1,p.field2))
                               .ToList();

^ I have something like that, but what i'm wondering, is there anyway way to add a second object creation, in the same select? eg. new MyObject(p.field3,p.field4) ? and add it to the same list? order does not matter.

I know could do this with multiple calls to database or splitting up lists into sections, but is there way to do this in single line?

4 Answers 4

5

You could create it as a tuple.

List<Tuple<MyObject1, MyObject2>> = query.Select(x => Tuple.Create(
    new MyObject1
    {
        // fields
    },
    new MyObject2
    {
        //fields
    }))
    .ToList();

From my testing in Linqpad, it seems that this will only hit the database once.

Alternatively, you could just select all the fields you know you'll need from the database to create both:

var myList = query.Select(x => new { FieldA = x.FieldA, FieldB = x.FieldB }).ToList(); //hits db once
var object1s = myList.Select(x => new MyObject1(x.FieldA));
var object2s = myList.Select(x => new MyObject1(x.FieldB));
var bothLists = object1s.Concat(object2s).ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

think that will end up being simplest/best way to just split. looked at tuple way but think would end up with list i need to rework anyway though. but tuple seems to be closest option. as i don't think what trying to do is possible :) (it ok to vote this up?)
anything that you find useful is okay to vote up, but you have have enough permission to vote things up. If this answered your question, click the check mark to signify so. That way, if others come to your question they'll see that this answered your question.
5

What you'd want to do is use the SelectMany method in linq. Which will select all the items from an array. The array can be created anonymously as seen below.

List<MyObject> objects = await item.tables.ToAsyncEnumerable()
                               .Where(p => p.field1 == value)
                               .SelectMany(p => new []{new MyObject(p.field1,p.field2), new MyObject(p.field3,p.field4)})
                               .ToList();

Hope that solves you problem!

Comments

1

If you use query syntax instead of method chaining, you can use the let operator to accomplish this. Note that the SQL generated may not be exactly performant as this article shows, but it should work for you if you're after a subquery.

Comments

1

You could try creating an array of objects and then flattening with SelectMany:

List<MyObject> objects = await item.tables.ToAsyncEnumerable()
                               .Where(p => p.field1 == value)
                               .Select(p => new [] {
                                            new MyObject(p.field1,p.field2),
                                            new MyObject(p.field3,p.field4)
                                            })
                               .SelectMany(g => g)
                               .ToList();

But I suspect you'll have problems getting EF to translate that to a query.

5 Comments

Perhaps it would work better if you did the SelectMany in memory.
@TravisJ Possibly, but I don't think that would be the problem - I don;t see how EF can return an array of objects.
Looks really neat that, is kind exactly what was looking to do. But compiler not liking the SelectMany, will have a mess about with it some more to see if it something i'm doing or just not possible. Thanks
Try my edit - I forgot that the projection is not optional in SelectMany.
Tried a few things with SelectMany. seems only way to do it is, retrieve the list from the database, into variable, then, do the selectmany on that. Not had chance to test it, or if dleh, solution is more effective. will post back when i do. 'code' List<MyObject> objects = queryedlist.SelectMany(p => new List<myobject> { new MyObject(p.field1,p.field2), new MyObject(p.field3,p.field4) }).ToList()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.