-1

LINQクエリに問題があります。テーブルtowarで取得したいKategorie.Nazwa

SQLの例:SELECT Kategorie.Nazwa FROM Towar INNER JOIN Kategorie ON Towar.Id_kat = Kategorie.Id_kat

ここに画像の説明を入力してください

これが私の問題.Where(p => category == null || p.Id_kat == category)です。カテゴリの名前が必要ですが、番号があります。p.Id_kat = SELECT Kategorie.Nazwa FROM Towar INNER JOIN Kategorie ON Towar.Id_kat = Kategorie.Id_katEntityFrameworkDbContextを使用してこれが必要です。

SomeView viewModel = new SomeView
                {
                    Towar = repository.Towar
                    .Where(p => category == null || p.Id_kat == category)
                    .OrderBy(p => p.Id_tow)
                    .Skip((page - 1) * PageSize)
                    .Take(PageSize),

                    Kategorie = re.Kategorie
                    .OrderBy(p => p.Id_kat),

                    PagingInfo = new PagingInfo
                                        {
                                       CurrentPage = page,
                                       ItemsPerPage = PageSize,
                                       TotalItems = repository.Towar.Count()

                                        },
                    CurrentCategory = category

                                   };
                return View(viewModel);
            }
4

1 に答える 1

2

TowarとKategoriesの間に外部キーを設定する場合、結合を行う必要はありません。KategoryクラスへのPOCOクラス参照とEntityFrameworkの機能を使用できます。これが私がテストで意味することの例です:

TowarクラスがKategoryクラスのプロパティを持っていることに注目してください。

[ Subject( "Inner join example using entity framework") ]
public class When_getting_towar_by_kategory_and_there_are_2_kategories
{
    Establish context = () =>
                        {
                            myContext = new MyContext();
                            Database.SetInitializer(new CreateDatabaseIfNotExists<MyContext>());
                            var first = new Kategory { Nazwa = "First" };
                            myContext.Kategories.Add(first);
                            var second = new Kategory { Nazwa = "Second" };
                            myContext.Kategories.Add(second);
                            myContext.Towars.Add(new Towar { Cena = "found", Kategory = first });
                            myContext.Towars.Add(new Towar { Cena = "notFound", Kategory = second });
                            myContext.SaveChanges();
                            SUT = new Controller(myContext);
                        };


    private Because of = () => { result = SUT.GetTowarByKategory("First"); };

    private It should_return_list_filtered_by_kategory = () => { result.Select(x => x.Cena).SequenceEqual(new[] { "found" }).Should().BeTrue(); };
    private static Controller SUT;
    private static IEnumerable<Towar> result;
    private static MyContext myContext;
}


public class Controller
{
    private readonly MyContext context;

    public Controller(MyContext context)
    {
        this.context = context;
    }
    public IEnumerable<Towar> GetTowarByKategory(string category)
    {
        var res = from t in context.Towars
                    where t.Kategory.Nazwa == category
                    select t;

        return res;
    }
}

public class Kategory
{
    [Key] 
    public int Id_kat { get; set; }
    public string Nazwa { get; set; }
}

public class Towar
{
    [Key]
    public int Id_tow { get; set; }
    public Kategory Kategory { get; set; }
    public string Cena { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Kategory> Kategories { get; set; }
    public DbSet<Towar> Towars { get; set; }
}
于 2012-08-06T09:53:41.273 に答える