Domanda

MODIFICA 1

Mi scuso ma dopo aver letto i 2 articoli suggeriti non capisco ancora cosa dovrei usare. Comprendo che l'utilizzo di IQueryable non è preferito per vari motivi, ma elimina anche IEnumerable? DataTable è davvero la mia migliore opzione?

In breve, immagino, qual è il tipo Return preferito?


Ho la seguente semplice query LINQ che desidero estrarre in un DAL. Qual è il tipo di var e quindi quale tipo dovrebbe essere il mio metodo?

            ConnectDBDataContext context = new ConnectDBDataContext();

        var lName = textEdit1.Text;

        var searchByPersonLName = from c in context.tblPersons
                                  where c.LastName == lName
                                  orderby c.LastName
                                  select new { c.FirstName,c.LastName,c.PersonID};

        dataGridView1.DataSource = searchByPersonLName;

Quando lo passo con il mouse su VS, viene visualizzato IQueryable < T > ma quando inserisco un breakpoint ed eseguo sembra chiamarsi IEnumerable. Qual è corretto e come devo dichiarare il mio metodo?

In questo modo - >

        public static DataTable SearchPerson(string SearhParam)
    {
        ConnectDBDataContext context = new ConnectDBDataContext();
        var persons = (from person in context.tblPersons
                       orderby person.LastName
                       select new { person.PersonID, person.LastName, person.FirstName, person.SSN });
        var filteredPersonsList = persons.Where(p => p.LastName == SearhParam).ToList();
        if (filteredPersonsList.Count == 0)
            filteredPersonsList = persons.Where(p => p.LastName.StartsWith(SearhParam)).ToList();

        var dataTable = filteredPersonsList.CopyLinqToDataTable();

        return dataTable;
    }

Se utilizzo IQueryable < T > che cos'è < T > o come faccio a saperlo e cosa restituirei?

Grazie!

Per riferimento, CopyToDataTable () è sotto.

public static DataTable CopyLinqToDataTable<T>(this IEnumerable<T> source)
    {
        return new ObjectShredder<T>().Shred(source, null, null);
    }

    public static DataTable CopyLinqToDataTable<T>(this IEnumerable<T> source,
                                                DataTable table, LoadOption? options)
    {
        return new ObjectShredder<T>().Shred(source, table, options);
    }
È stato utile?

Soluzione

Ciò che intende è mappare i tuoi dati sull'oggetto che desideri restituire dal DAL.

In risposta alla tua prima domanda " var " è davvero l'abbreviazione di variabile, e il tipo è ciò che mai è definito nell'assegnazione.

var myvariable = string.empty;

In questo esempio il tipo è quello di una stringa.

var myreader = new StringReader();

Mentre in questo esempio il tipo è quello di un StringReader.

Per quanto riguarda la tua seconda domanda di " che cos'è " ;. T è un tipo generico.

Per un esempio di dove il tuo dal dovrebbe restituire un oggetto reale:

 public Product GetProduct(int ProductID)
    {
        var product = from p in db.MyTable
                      where p.productID == ProductID
                      select new product { name = p.name, pricepoint = p.pricepoint, qty = p.quantity };

        return product;
    }

Altri suggerimenti

Prima di tutto, IQueryable implementa IEnumerable, quindi è per questo che potresti potenzialmente vedere entrambi. Vedi qui per maggiori dettagli

In generale, consiglierei al tuo DAL di restituire i tuoi oggetti reali quando possibile.

Vorrei leggere questo blog per linee guida su come e come non fare ciò che stai suggerendo. Risposta breve, non restituire IQueryable.

EDIT: Esempio:

        internal static File[] GetAllFilesByUserID(int userID)
    {
        var db = GetDataContext();
        return (from files in db.Files where files.OwnerUserID == userID select files).ToArray();
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top