Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq查询-实体框架(连接两个表)_C#_Asp.net Mvc_Asp.net Mvc 3_Linq_Entity Framework - Fatal编程技术网

C# Linq查询-实体框架(连接两个表)

C# Linq查询-实体框架(连接两个表),c#,asp.net-mvc,asp.net-mvc-3,linq,entity-framework,C#,Asp.net Mvc,Asp.net Mvc 3,Linq,Entity Framework,我对LINQ查询有问题。在towar桌上我想要Kategorie,Nazwa sql中的示例:从Towar.Id_kat=Kategorie.Id_kat上的Towar内部连接Kategorie中选择Kategorie.Nazwa 这是我的问题。其中p=>category==null | | p.Id_kat==category我需要类别的名称,但我有编号。我需要这个p.Id_kat=SELECT Kategorie.Nazwa从Towar内部使用EntityFramework DbContex

我对LINQ查询有问题。在towar桌上我想要Kategorie,Nazwa

sql中的示例:从Towar.Id_kat=Kategorie.Id_kat上的Towar内部连接Kategorie中选择Kategorie.Nazwa


这是我的问题。其中p=>category==null | | p.Id_kat==category我需要类别的名称,但我有编号。我需要这个p.Id_kat=SELECT Kategorie.Nazwa从Towar内部使用EntityFramework DbContext在Towar.Id_kat=Kategorie.Id_kat I上连接Kategorie

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);
            }

如果在Towar和Kategories之间设置外键,则不必进行连接。您可以使用POCO类引用Kategory类和powerofentity框架。下面是一个关于测试的示例:

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

您的类别变量是存储在Kategorie.Nazwa中的值吗?Kategorie.Nazwa-nvarchar50p.Id_kat=从Towar.Id_kat=Kategorie.Id_kat上的Towar内部连接Kategorie中选择Kategorie.Nazwa,这是什么意思?现在p.Id_kat是相等的数字,但我需要这样的东西->从Towar内部选择Kategorie.Nazwa在Towar上加入Kategorie.Id_kat=Kategorie.Id_kat我需要类别名称,但现在我有了类别Id号