-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_kat我使用EntityFramework DbContext。

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 和 Categories 之间设置了外键,则不必进行连接。您可以使用 POCO 类引用到类别类和实体框架的强大功能。这是我对测试的意思的一个例子:

请注意 Towar 类如何具有对 Category 类的属性。

[ 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 回答