“LINQ to Entities”中的“不支持”成员 [英] Member is "not supported in LINQ to Entities"

查看:131
本文介绍了“LINQ to Entities”中的“不支持”成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是C#,LINQ和MVC的新手。我正在尝试获取一份年龄列表,但是它表示


LINQ to Entities不支持指定的类型成员Age。
只支持初始化器,实体成员和实体导航属性


对于上一个教程,使用这个完全相同的逻辑,除了它们检查字符串,而不是int(Age)。为什么这会给我一个合适的,我该如何解决?

  public ActionResult SearchIndex(string ageValue,string searchString)
{
if(!string.IsNullOrEmpty(ageValue))
{
var AgeList = new List< string>();
var AgeListQry = from d in db.Actors orderby d.Age select d.Age.ToString();
AgeList.AddRange(AgeListQry.Distinct());
}
//其他东西
}

我想了解发生了什么,以便将来可以避免这种情况。



实体模型代码

  public class Actor 
{
public int ID {get;组; }
public string Name {get;组; }
public DateTime BirthDate {get;组; }
public int Age
{
get {
return(int)(DateTime.Now - BirthDate).TotalDays / 365;
}

}
public decimal NetValue {get;组; }
}
public class ActorDBContext:DbContext
{
public DbSet< Actor>演员{get;组; }
}


解决方案

,您不能在Linq to Entities查询中调用 ToString()。而是这样做:

  var AgeList = new List< string>(); 
//作为任何类型的年龄检索,在SQL Server中没有转换
var AgeListQry =(from d in db.Actors orderby d.Age select d.Age).ToList();
//在事实之后转换它们,使用Linq to Objects
AgeList.AddRange(AgeListQry.Select(a => a.ToString())。Distinct());

编辑



我看到你最新的更新,显示 Age 不是数据库列。然后,您需要执行此操作(假设 BirthDate 正确映射):

  var AgeList = new List< string>(); 
//从SQL Server检索BirthDate,并使用ToList()使其立即运行
var AgeListQry =(从db.Actors orderby d.BirthDate中的d选择d.BirthDate).ToList();
//在事实之后转换它们,使用Linq to Objects
AgeList.AddRange(AgeListQry.Select(bd =>((int))(DateTime.Now - bd).TotalDays / 365).ToString ())。不同());

Linq to Entities将您的表达式映射到SQL语句,并且在使用时无法映射到您的年龄属性。相反,您需要从SQL Server( BirthDate )获取您可以的内容,然后将其翻译成Age。您可以使用这样的方法调用替换内嵌代码:

  AgeList.AddRange(AgeListQry.Select bd => CalculateAge(bd))。Distinct()); 
// ...
private string CalculateAge(DateTime birthday)
{
return((int)(DateTime.Now - bd).TotalDays / 365).ToString() ;
}


So I am new to C#, LINQ, and MVC. I am trying to get a list of Ages, but it says

The specified type member 'Age' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

For a previous tutorial, they use this exact same logic, except they check a string, not an int (Age). Why is this giving me a fit, and how can I fix it?

public ActionResult SearchIndex(string ageValue, string searchString)
{
    if (!string.IsNullOrEmpty(ageValue))
    {
         var AgeList = new List<string>();
         var AgeListQry = from d in db.Actors orderby d.Age select d.Age.ToString();
         AgeList.AddRange(AgeListQry.Distinct());
    }
    // other stuff
}

I want to learn what is going on, so that I can avoid this in the future!

Entity Model code

public class Actor
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime BirthDate { get; set; }
        public int Age
        {
            get { 
                return (int)(DateTime.Now - BirthDate).TotalDays / 365; 
            }

        }
        public decimal NetValue { get; set; }
    }
    public class ActorDBContext : DbContext
    {
        public DbSet<Actor> Actors { get; set; }
    }

解决方案

As mentioned in the comments, you can't call ToString() in a Linq to Entities query. Instead do it like this:

var AgeList = new List<string>();
//retrieve as whatever type Age is, no conversion in SQL Server
var AgeListQry = (from d in db.Actors orderby d.Age select d.Age).ToList();
//convert them after the fact, using Linq to Objects
AgeList.AddRange(AgeListQry.Select(a => a.ToString()).Distinct());

EDIT

I saw your latest update that does show that Age is not a database column. You are then required to do something like this (assuming BirthDate is properly mapped):

var AgeList = new List<string>();
//retrieve BirthDate from SQL Server and use ToList() to get it to run immediately
var AgeListQry = (from d in db.Actors orderby d.BirthDate select d.BirthDate).ToList();
//convert them after the fact, using Linq to Objects
AgeList.AddRange(AgeListQry.Select(bd => ((int)(DateTime.Now - bd).TotalDays / 365).ToString()).Distinct());

Linq to Entities maps your expressions to SQL statements and there is nothing for it to map to when you use your Age property. Instead, you need to get what you can from SQL Server (BirthDate) and then do the translation to Age yourself. You could replace the inline code with a method call like this if you'd rather:

AgeList.AddRange(AgeListQry.Select(bd => CalculateAge(bd)).Distinct());
//...
private string CalculateAge(DateTime birthday)
{
   return ((int)(DateTime.Now - bd).TotalDays / 365).ToString();
}

这篇关于“LINQ to Entities”中的“不支持”成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆