私はBrand
ワイズトップ販売3項目を示すLINQでクエリを書く必要があります。 私の結果はどのようにLINQでこのクエリを記述する誰も助けてくださいすることができます。このブランド賢明なトップセリング3アイテムLINQを使用
ようなものです。
よろしく、 Ajith
私はBrand
ワイズトップ販売3項目を示すLINQでクエリを書く必要があります。 私の結果はどのようにLINQでこのクエリを記述する誰も助けてくださいすることができます。このブランド賢明なトップセリング3アイテムLINQを使用
ようなものです。
よろしく、 Ajith
あなたはOrderByDescを組み合わせて、コレクション内の3つの最高のアイテムを取得するために取る必要があります。ブランドごとに結果を表示する場合は、クエリにGroupByステートメントを追加する必要があります。
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<Brand> brands = new List<Brand>()
{
new Brand() {Name = "Brand 1", Item = "Item1", Qty = 200},
new Brand() {Name = "Brand 1", Item = "Item1", Qty = 100},
new Brand(){Name = "Brand 1", Item = "Item3", Qty = 98},
new Brand() {Name = "Brand 1", Item = "Item4", Qty = 95},
new Brand() {Name = "Brand 2", Item = "Item5", Qty = 150},
new Brand() {Name = "Brand 2", Item = "Item6", Qty = 125},
new Brand() {Name = "Brand 2", Item = "Item7", Qty = 120}};
Console.WriteLine("Top items for all ");
var topOfAll = brands.OrderByDescending(x => x.Qty).Take(3);
foreach(Brand brand in topOfAll)
{
Console.WriteLine(brand.Name + " " + brand.Item + " - " + brand.Qty);
}
IEnumerable<IGrouping<string, Brand>> grouped = brands.GroupBy(x => x.Name);
foreach (IGrouping<string, Brand> groupedBrand in grouped)
{
Console.WriteLine("Top items for " + groupedBrand.Key);
IEnumerable<Brand> top = groupedBrand.Select(x => x).OrderByDescending(x => x.Qty).Take(3);
foreach (Brand i in top)
{
Console.WriteLine(i.Name + " " + i.Item + " - " + i.Qty);
}
}
}
}
public class Brand
{
public string Name
{
get;
set;
}
public string Item
{
get;
set;
}
public int Qty
{
get;
set;
}
}
このサンプルの結果は次のとおりです:
Top items for all Brand 1 Item1 - 200 Brand 2 Item5 - 150 Brand 2 Item6 - 125 Top items for Brand 1 Brand 1 Item1 - 200 Brand 1 Item1 - 100 Brand 1 Item3 - 98 Top items for Brand 2 Brand 2 Item5 - 150 Brand 2 Item6 - 125 Brand 2 Item7 - 120
はここで、この例のfiddleだ
は、ここでの例です。
OPの側から何の努力も見ずに答えを出すのは残念です.... –
Linqでの単一の問い合わせで可能です。しかし、私は単一の生のSQLコマンドで作った。 Thanks Buddies:P –
@GiladGreenありがとうございます。:P –
結果は以下のようになります。
IQueryable<Sales> query = null;
StringBuilder Query = new StringBuilder();
Query.Clear();
Query.Append("select *" + Environment.NewLine);
Query.Append("from" + Environment.NewLine);
Query.Append("(" + Environment.NewLine);
Query.Append(" select brand, item, sum(qty) as qty, ROW_NUMBER() over(partition by brand order by sum(qty) desc) as row_num" + Environment.NewLine);
Query.Append(" from sales" + Environment.NewLine);
Query.Append(" group by brand, item" + Environment.NewLine);
Query.Append(")t" + Environment.NewLine);
Query.Append("where row_num <= 3" + Environment.NewLine);
Query.Append("order by brand asc, qty desc" + Environment.NewLine);
DataTable dt = GetDataTable(db, Query.ToString());
query = (
from rw in dt.AsEnumerable()
select new Sales
{
item = rw.Field<string>("item"),
brand = rw.Field<string>("brand"),
qty = rw.Field<int>("qty")
}
).AsQueryable();
public DataTable GetDataTable(DbContext context, string sqlQuery)
{
DbProviderFactory dbFactory =
DbProviderFactories.GetFactory(context.Database.Connection);
using (var cmd = dbFactory.CreateCommand())
{
cmd.Connection = context.Database.Connection;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlQuery;
using (DbDataAdapter adapter = dbFactory.CreateDataAdapter())
{
adapter.SelectCommand = cmd;
DataTable dt = new DataTable();
adapter.Fill(dt);
return dt;
}
}
}
このクエリはMS SQL Serverでのみ動作することを願います。 もう一度お礼を申し上げます..:
あなたのオブジェクトと試したことを表示しますか?.. –
「何も試してみませんでした。 – Mardoxx
私たちはここにあなたのためのコードを書くのではありません。 [尋ねる] – MickyD