Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
Asp.net mvc 如何在mvc中使用linq查询给出多个where条件_Asp.net Mvc_Linq - Fatal编程技术网

Asp.net mvc 如何在mvc中使用linq查询给出多个where条件

Asp.net mvc 如何在mvc中使用linq查询给出多个where条件,asp.net-mvc,linq,Asp.net Mvc,Linq,我想在使用linq在mvc中绑定数据时给出多个where条件。我尝试了与sql查询和其他类型相同的where条件,但也不起作用,并显示了错误,即 LINQ to中不支持指定的类型成员“YearID” 实体。仅初始值设定项、实体成员和实体导航 支持属性 这是我最近试过的代码 public ActionResult FilterImage(int yearID, int eventID, int branchID) { var content = db.eventRegistration.

我想在使用linq在mvc中绑定数据时给出多个where条件。我尝试了与sql查询和其他类型相同的where条件,但也不起作用,并显示了错误,即

LINQ to中不支持指定的类型成员“YearID” 实体。仅初始值设定项、实体成员和实体导航 支持属性

这是我最近试过的代码

 public ActionResult FilterImage(int yearID, int eventID, int branchID)
{
    var content = db.eventRegistration.Select(s => new
    {
        s.EventRegistrationID,
        s.Image,
        s.IsActive
    });

    List<EventRegistrationViewModel> contentModel = content
       .Select(item => new EventRegistrationViewModel() 
       {
            EventRegistrationID = item.EventRegistrationID,
            Image = item.Image,
            IsActive = item.IsActive
       })
       .Where(c => c.IsActive == true && c.YearID == yearID && c.BranchID == branchID)
       .Take(15).ToList();

    return View(contentModel);
}
public ActionResult过滤器映像(int yearID、int eventID、int branchID)
{
var content=db.eventRegistration.Select(s=>new
{
s、 EventRegistrationID,
s、 形象,,
s、 活跃的
});
列表contentModel=content
.选择(项=>new EventRegistrationViewModel()
{
EventRegistrationID=item.EventRegistrationID,
Image=item.Image,
IsActive=item.IsActive
})
.Where(c=>c.IsActive==true&&c.YearID==YearID&&c.BranchID==BranchID)
.取(15)ToList();
返回视图(contentModel);
}
此代码不适用于多个where条件

我知道,以前几乎有人问过这个问题,但答案是这个解决方案不起作用


谢谢。

我不知道您在尝试什么,但解决方案在
内容
列表中,您必须选择
YearID
BranchID
,因为在
中您正在使用它

我不知道你是否需要所有的属性,这是不同的讨论,但现在你可以像下面这样做

 public ActionResult FilterImage(int yearID, int eventID, int branchID)
{
    var content = db.eventRegistration.Select(s => new
    {
        s.EventRegistrationID,
        s.Image,
        s.IsActive,
        s.YearID,
        s.BranchID
    }).ToList();

    List<EventRegistrationViewModel> contentModel = content.Select(item => new EventRegistrationViewModel()
    {
        EventRegistrationID = item.EventRegistrationID,
        Image = item.Image,
        IsActive = item.IsActive
    }).Where(c => c.IsActive == true && c.YearID == yearID && c.BranchID == branchID).Take(15).ToList();

    return View(contentModel);
}
public ActionResult过滤器映像(int yearID、int eventID、int branchID)
{
var content=db.eventRegistration.Select(s=>new
{
s、 EventRegistrationID,
s、 形象,,
s、 我很活跃,
s、 伊莱德,
s、 布兰奇德
}).ToList();
List contentModel=content.Select(item=>neweventregistrationviewmodel()
{
EventRegistrationID=item.EventRegistrationID,
Image=item.Image,
IsActive=item.IsActive
})其中(c=>c.IsActive==true&&c.YearID==YearID&&c.BranchID==BranchID).Take(15).ToList();
返回视图(contentModel);
}

我做了同样的事情,也在我的问题中描述了同样的事情,因此它显示了错误。哦,我想我知道了,我忘了添加
ToList()
内容中
请参见更新answer@Curiousdev这会将整个数据库表带到客户端,而不在数据库端应用任何过滤哦,是的,我知道@SergeyBerezovskiy,这就是为什么我已经提到了他在用这个做什么,我完全不知道awared@Curiousdev同样的代码,我做了正确地看到我的问题。问题是不是在多种情况下。问题是在使用
YearID
属性的单一条件下。你能说明这个问题的解决方案吗?你应该说明属性
YearID
在你的实体中是如何定义的。对不起……我不明白……??我还看到你说的问题是重复的,但没有解决方案得到我的问题解决方案。