LINQ to Objects - Philosophy of Use?

I'm starting to learn LINQ for objects, but I want to know how application design can best fit LINQ for objects.

As an example ... the application displays only female employees, and the employee table in the database contains data for both men and women.

By default, the application creates an SQL statement to retrieve only female employees from the database and places them in an object (list), which is then passed through the application for display, etc.

To get the most influence on LINQ for objects, I assume that you will first need to get all the data for employees, and then apply LINQ to filter the object to display only female employees. The advantage is that you can continue to query employees without touching the database again.

This is a basic example, but the point I'm trying to make is where the line intersects between the gain and the effective lead time.

What if the employee table contains 100,000 records?

I see the benefits of LINQ .... but in my head I am convinced that first you need to put ALL the data in an object from which you can use LINQ to query.

Getting ALL data can be too expensive to make it useful when I can just query the database again every time I need a different subset of data.

Thoughts?

+3
source share
6 answers

If you only need a subset of data to get you started, select only that subset. I don’t think it makes sense to get all the employee data in your example.

However, this does not mean that LINQ to Objects is rarely useful - often when you already have some data (which may well not be from the database - I find it incredibly useful with reflection, for example), you want to cut and cut it into several ways. LINQ to Objects is a very powerful tool for this.

LINQ LINQ. , ( ) . , Java - , - , LINQ to Objects .

+2

LINQ - .

, ( ) ( , XML, ..). , , - ,

, , #. SQL , # ( , )

LINQ , , SQL // . SQL .

EDIT: , , . - //, , ?

- - , , ( SQL)?

+1

, , LINQ-to-Objects . , , , , LINQ to Objects , .

, DAL , , FindAll DAL, LINQ . ( ).

LINQ-to-Objects . , , , .

+1

LINQ , . , LINQ /XML/etc ; , , , . ; ( ), , . , LINQ ; , .

, , , . :

public static string RemovePunctuation(this string @string)
{
    if (string.IsNullOrEmpty(@string))
        return @string;

    var bytes = (
        from ch in @string
        where !Char.IsPunctuation(ch) && !Char.IsWhiteSpace(ch)
        select Convert.ToByte(ch)
        ).ToArray(); // <- calling to array enumerates the results

    return UnicodeEncoding.ASCII.GetString(bytes);
}

, , LINQ , .

+1

? , .

, . , , . .

, , , , , Linq .

... , , , , , , .

: , .

var lines = File.ReadAllLines(fileName);

, lines , IEnumerable, Linq . , , :

var nonBlankLines = lines.Where(line => line.Trim() == string.Empty);

, ( - !):

var quoted = lines.Where(line => line.Trim() == string.Empty)
                  .Select(line => "\"" + line + "\"");

( , .)

- , :

var quoted = File.ReadAllLines(fileName)
                 .Where(line => line.Trim() == string.Empty)
                 .Select(line => "\"" + line + "\"");

, , , , Join, , :

var quoted = string.Join(", ", 
                         File.ReadAllLines(fileName)
                             .Where(line => line.Trim() == string.Empty)
                             .Select(line => "\"" + line + "\"")
                             .ToArray());

Linqy:

var quoted = File.ReadAllLines(fileName)
                 .Where(line => line.Trim() == string.Empty)
                 .Select(line => "\"" + line + "\"")
                 .Aggregate((a, b) => a + ", " + b);

, , ( , ). , , - Aggregate, Util.Generate:

IEnumerable<T> Generate<T>(T item, Func<T, T> generator)
{
    for (; item != null; item = generator(item))
        yield return item;
}

, , . Exception.InnerException, , - . , x:

MessageBox.Show(Util.Generate(x, i => i.InnerException).Last().Message);

Generate IEnumerable, Linq . , , .

, , , , , , , , .

+1

LinqToObjects 100 000 . foreach.

, LinqToObjects foreach . foreach , LinqToObjects.

+1

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


All Articles