How to: Return Subsets of Element Properties in a Query 


Мы поможем в написании ваших работ!



ЗНАЕТЕ ЛИ ВЫ?

How to: Return Subsets of Element Properties in a Query



Use an anonymous type in a query expression when both of these conditions apply:

· You want to return only some of the properties of each source element.

· You do not have to store the query results outside the scope of the method in which the query is executed.

If you only want to return one property or field from each source element, then you can just use the dot operator in the select clause. For example, to return only the ID of each student, write the select clause as follows:

select student.ID;

Example

The following example shows how to use an anonymous type to return only a subset of the properties of each source element that matches the specified condition.

private static void QueryByScore() { // Create the query. var is required because // the query produces a sequence of anonymous types. var queryHighScores = from student in students where student.ExamScores[0] > 95 select new { student.FirstName, student.LastName }; // Execute the query. foreach (var obj in queryHighScores) { // The anonymous type's properties were not named. Therefore // they have the same names as the Student properties. Console.WriteLine(obj.FirstName + ", " + obj.LastName); } } /* Output: Adams, Terry Fakhouri, Fadi Garcia, Cesar Omelchenko, Svetlana Zabokritski, Eugene */

Возвращение поднаборов свойств элементов в запросе

Используйте анонимный тип в выражении запроса, если выполняются оба этих условия:

· Требуется возвратить только некоторые свойства каждого из исходных элементов.

· Не требуется сохранять результаты запроса за пределами области действия метода, в котором был выполнен запрос.

Если требуется возвратить одно свойство или поле из каждого исходного элемента, можно просто воспользоваться оператором "точка" в предложении select. Например, чтобы возвратить только ID для каждого student, создайте следующее предложение select:

select student.ID;

Пример

В следующем примере показывается, как использовать анонимный тип для возврата только поднабора свойств каждого из исходных элементов, соответствующих указанному условию.

ß----


Note that the anonymous type uses the source element's names for its properties if no names are specified. To give new names to the properties in the anonymous type, write the select statement as follows:

select new { First = student.FirstName, Last = student.LastName };

If you try this in the previous example, then the Console.WriteLine statement must also change:

Console.WriteLine(student.First + " " + student.Last);

Compiling the Code

· To run this code, copy and paste the class into a Visual C# console application project that has been created in Visual Studio. By default, this project targets version 3.5 of the.NET Framework, and it will have a reference to System.Core.dll and a using directive for System.Linq. If one or more of these requirements are missing from the project, you can add them manually.

 


Обратите внимание, что анонимный тип использует имена исходного элемента для его свойств, если имена не заданы. Чтобы назначить свойствам в анонимном типе новые имена, укажите следующий оператор select:

select new { First = student.FirstName, Last = student.LastName };

Если попытаться вставить его в предыдущий пример, то потребуется изменить и оператор Console.WriteLine:

Console.WriteLine(student.First + " " + student.Last);

Компиляция кода

· Для выполнения этого кода скопируйте класс в проект консольного приложения на языке Visual C#, которое было создано в среде разработки Visual Studio. По умолчанию этот проект предназначен для версии 3.5 платформы.NET Framework и имеет ссылку на библиотеку System.Core.dll и директиву using для System.Linq. Если одно или более требований в проекте отсутствуют, их можно добавить вручную.

 


Extension Methods

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

The most common extension methods are the LINQ standard query operators that add query functionality to the existing System.Collections..::.IEnumerable and System.Collections.Generic..::.IEnumerable<(Of <(T>)>) types. To use the standard query operators, first bring them into scope with a using System.Linq directive. Then any type that implements IEnumerable<(Of <(T>)>) appears to have instance methods such as GroupBy, OrderBy, Average, and so on. You can see these additional methods in IntelliSense statement completion when you type "dot" after an instance of an IEnumerable<(Of <(T>)>) type such as List<(Of <(T>)>) or Array.

The following example shows how to call the standard query operator OrderBy method on an array of integers. The expression in parentheses is a lambda expression. Many standard query operators take lambda expressions as parameters, but this is not a requirement for extension methods.

class ExtensionMethods2 {   static void Main() { int[] ints = { 10, 45, 15, 39, 21, 26 }; var result = ints.OrderBy(g => g); foreach (var i in result) { System.Console.Write(i + " "); } } } //Output: 10 15 21 26 39 45

Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.


Методы расширения

Метода расширения позволяют "добавлять" методы в существующие типы без создания нового производного типа, перекомпиляции или иного изменения исходного типа. Методы расширения являются особым видом статического метода, но они вызываются, как если бы они были методами экземпляра в расширенном типе. Для клиентского кода, написанного на языках C# и Visual Basic, нет видимого различия между вызовом метода расширения и вызовом методов, фактически определенных в типе.

Наиболее распространенными методами расширения являются стандартные операторы запроса LINQ, добавляющие функции запроса в существующие типы System.Collections..::.IEnumerable и System.Collections.Generic..::.IEnumerable<(Of <(T>)>). Для использования стандартных операторов запроса их сначала надо добавить в область видимости с помощью директивы using System.Linq. Затем каждый тип, который реализует тип IEnumerable<(Of <(T>)>), будет иметь методы экземпляра, такие как GroupBy, OrderBy, Average и т.д. Эти дополнительные методы можно видеть в завершении операторов IntelliSense, когда вводится точка после экземпляра типа IEnumerable<(Of <(T>)>), например List<(Of <(T>)>) или Array.

В следующем примере показано, как вызывать метод стандартного оператора запроса OrderBy для массива целых чисел. Выражение в скобках называется лямбда-выражением. Многие стандартные операторы запроса принимают лямбда-выражения в качестве параметров, но это не является обязательным для методов расширения. Дополнительные сведения см. в разделе Лямбда-выражения.

ß------

Методы расширения определяются как статические методы, но вызываются с помощью синтаксиса обращения к методу экземпляра. Их первый параметр определяет, с каким типом оперирует метод, и перед параметром идет модификатор this. Методы расширения находятся в области действия, только если пространство имен было явно импортировано в исходный код с помощью директивы using.

 


The following example shows an extension method defined for the System..::.String class. Note that it is defined inside a non-nested, non-generic static class:

namespace ExtensionMethods { public static class MyExtensions { public static int WordCount(this String str) { return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length; } } }

The WordCount extension method can be brought into scope with this using directive:

using ExtensionMethods;

And it can be called from an application by using this syntax:

string s = "Hello Extension Methods"; int i = s.WordCount();

In your code you invoke the extension method with instance method syntax. However, the intermediate language (IL) generated by the compiler translates your code into a call on the static method. Therefore, the principle of encapsulation is not really being violated. In fact, extension methods cannot access private variables in the type they are extending.

In general, you will probably be calling extension methods far more often than implementing your own. Because extension methods are called by using instance method syntax, no special knowledge is required to use them from client code. To enable extension methods for a particular type, just add a using directive for the namespace in which the methods are defined. For example, to use the standard query operators, add this using directive to your code:

using System.Linq;

(You may also have to add a reference to System.Core.dll.) You will notice that the standard query operators now appear in IntelliSense as additional methods available for most IEnumerable<(Of <(T>)>) types.

Note:
Although standard query operators do not appear in IntelliSense for String, they are still available.

В приведенном ниже примере показан метод расширения, определяемый для класса System..::.String. Обратите внимание, что этот метод определяется внутри не вложенного, не универсального статического класса.

ß---

Метод расширения WordCount может быть добавлен в область действия с помощью директивы using.

using ExtensionMethods;

И он может вызываться из приложения с помощью следующего синтаксиса.

string s = "Hello Extension Methods"; int i = s.WordCount();

В созданном коде метод расширения вызывается с помощью синтаксиса обращения к метода экземпляра. Однако промежуточный язык (IL), генерируемый компилятором, переводит созданный код в вызов статического метода. Поэтому принцип инкапсуляции фактически не нарушается. В действительности, методы расширения не могут получить доступ к закрытым переменным типа, для расширения которого они используются.

Вообще, обычно гораздо чаще приходится вызывать методы расширения, чем реализовывать собственные методы. Так как методы расширения вызываются с помощью синтаксиса обращения к методу экземпляра, для использования их из клиентского кода специальные знания не требуются. Чтобы включить методы расширения для определенного типа необходимо просто добавить директиву using для пространства имен, в котором эти методы определяются. Например, чтобы использовать стандартные операторы запроса, необходимо добавить директиву using в создаваемый код.

using System.Linq;

(Также может потребоваться добавить ссылку на библиотеку System.Core.dll.) Обратите внимание, что стандартные операторы запроса теперь появляются в списке IntelliSense в виде дополнительных методов, доступных для большинства типов IEnumerable<(Of <(T>)>).



Поделиться:


Последнее изменение этой страницы: 2017-01-19; просмотров: 94; Нарушение авторского права страницы; Мы поможем в написании вашей работы!

infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 3.141.200.180 (0.02 с.)