Linq查询 - 实体框架(加入两个表)

时间:2012-08-06 08:51:11

标签: c# asp.net-mvc asp.net-mvc-3 linq entity-framework

我遇到LINQ查询问题。在表格towar中,我想获得Kategorie.Nazwa

sql中的示例:SELECT Kategorie.Nazwa FROM Towar INNER JOIN Kategorie ON Towar.Id_kat = Kategorie.Id_kat

enter image description here

这是我的问题.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);
            }

1 个答案:

答案 0 :(得分:2)

如果在Towar和Kategories之间设置外键,则无需进行连接。您可以使用POCO类引用Kategory类和Entity Framework的强大功能。以下是我对测试的意思的一个例子:

注意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; }
}