DataConnector
LINQ クエリ
ADO.NET provider for Magento > LINQ クエリ

LINQ queries demonstrate how to operate and query the Magento objects wrapped in an Entity Framework data model. Below are some examples of LINQ queries supported by the Entity framework.

Select and Filter

Retrieve records from the Products table where the Product Name is "Vintage Backpack".

C#
コードのコピー
var records = from p in context.Products
            where p.Code== "Vintage Backpack"
            select new { p.Id, p.Name, p.Price};
Contains

Retrieve records from the CustomerGroups table where the Code property contains the substring "Retailer".

C#
コードのコピー
var records = context.CustomerGroups.Where(x => x.Code.Contains("Retailer")); 
Limit

Retrieve the first 10 records from the Products table.

C#
コードのコピー
var records = (from p in context.Products
            select p).Take(10); // レコードを10 件取得します。
Order by

Retrieve all records from the CustomerGroups table and sorts them in descending order based on the Code property.

C#
コードのコピー
var records = (from p in context.CustomerGroups
            orderby p.Code descending//Order Byの実装
            select p);
Count

Count all entities that match a given criterion.

C#
コードのコピー
var _count = (from p in context.Products 
            select p).Count();//選択されたレコードの数に基づいてクエリをカウントします。
Group By

Group records from the Products table based on the Price property. The groups are then ordered in descending order based on the Price.

C#
コードのコピー
var sampleTable = context.Products.AsEnumerable();
var queryCreatedBy = from b in sampleTable
                    group b by b.Price  into newGroup //価格に基づいた Linq グループ クエリ。 
                    orderby newGroup.Key descending
                    select newGroup;
Joins

Cross join between the Products and CustomerGroups tables.

C#
コードのコピー
var records = from b in context.Products 
            from e in context.CustomerGroups
            select new { b, e };//クロス結合を定義します。