如何使用LINQ从列表中的列表中获取所有对象

时间:2016-04-28 13:02:22

标签: c# linq

我有一份汽车清单,他们的生产工厂中有一份清单。

 package tfe.rma.ciss.be;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlrpc.android.MethodCall;
import org.xmlrpc.android.XMLRPCServer;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class Server extends IntentService {
    public String myData="";
    public String streamTitle = "",path="";

    public void onCreate() {
        super.onCreate();
        Log.d("Server", ">>>onCreate()");
    }

    public Server() {
        super("Server");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, startId, startId);
        Log.i("LocalService", "Received start id " + startId + ": " + intent);

        return START_STICKY;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("Server", ">>>handlingIntent()");
        try {
            ServerSocket socket = new ServerSocket(8214);
            XMLRPCServer server = new XMLRPCServer();
            Log.d("Server", ">>>opening on port" + socket);

            while (true) {
                Socket client = socket.accept();
                MethodCall call = server.readMethodCall(client);
                String name = call.getMethodName();

                if (name.equals("newImage")) {
                    ArrayList<Object> params = call.getParams();
                    // assume "add" method has two Integer params, so no checks done
                    myData = (String)( params.get(0));
                    //int i1 = (Integer) params.get(1);
                    server.respond(client, new Object[] {200});
                    /*intent = new Intent (this, ParseFunction.class);
                    startService (intent);  */

                    Toast.makeText(this, myData, Toast.LENGTH_SHORT).show();  
                    Log.d("ParseFunction", ">>>Started()"); 

                    Intent i = new Intent( this, B.class );
                    i.putExtra( "Azo", myData);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity( i );
                } else {
                    server.respond(client, null);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }
}

现在我在这个列表中列出了每辆车都处于生产状态的因素。

List<Car> cars = new List();

现在我想获得一个包含单个工厂的列表,以便我拥有特定汽车正在生产的所有工厂。

例如:

List<FactoryCollection> factoryCollections = cars
    .Select(c => c.Factories)
    .ToList();

我的问题是物体更深一层。

我的问题是:

如何使用LINQ语句获取对象,或者有更简单的方法来获取所有工厂?

3 个答案:

答案 0 :(得分:5)

使用SelectMany代替Select

List<Factory> factories = Car.Select(x => x.Factories).SelectMany(y => y.Werk).ToList();

答案 1 :(得分:5)

看来,您正在寻找SelectMany

  List<Car> Car = new List<Car>() {... }
  ...
  var factories = Car
    .Where(car => IsCarSpecific(car))
    .SelectMany(car => car.Factories
       .Select(factory => factory.Werk));
    // .Distinct(); // in case you want distinct factories
    // .ToList(); if you want materiazlize into List<Factory>

答案 2 :(得分:3)

您正在寻找IQueryable<{CreateNewSelectObjects}> query = ... ; if (sProjType != null) { query = query.Where(x => x.TypeDesc==sProjType); } ,但与其他有些嵌套的答案相反:

SelectMany