C# 在Linq到SQL中转换

C# 在Linq到SQL中转换,c#,linq-to-sql,casting,C#,Linq To Sql,Casting,我有一个接口具有以下方法签名: public interface ITag { int M_Id { get; set; } string M_Name { get; set; } } 我有一个实现上述接口的类: [Table(Name="News")] public class NewsTag:ITag { [Column(Name="id",isPrimaryKey = true)] public int M_Id { get; se

我有一个接口具有以下方法签名:

public interface ITag
{
    int M_Id { get; set; }
    string M_Name { get; set; }
}
我有一个实现上述接口的类:

[Table(Name="News")]
public class NewsTag:ITag
{
    [Column(Name="id",isPrimaryKey = true)]
    public int M_Id
    {
        get; set;
    }

    [Column(Name = "name")]
    public string M_Name
    {
        get; set;
    }

    [Column(Name = "extraField")]
    public string M_ExtraField
    {
        get;
        set;
    }
}
我想从数据库中检索行并发送它们

public IQueryable<ITag> fGetNewsTags(int id)
{
    var result = from news in context.GetTable<NewsTag>()
                 where news.M_Id == id
                 select news;

    return (IQueryable<ITag>)result;
}
public IQueryable fGetNewsTags(int-id)
{
var result=来自context.GetTable()中的新闻
其中news.M_Id==Id
选择新闻;
返回(IQueryable)结果;
}
但问题是,我尝试使用的casting(return(IQueryable)result;)不起作用,即使NewsTag是ITag接口的子类

任何帮助都将不胜感激。

请尝试以下内容

public IQueryable<ITag> fGetNewsTags(int id)
{
    var result = from news in context.GetTable<NewsTag>()
                 where news.M_Id == id
                 select (ITag)news;

    return result;
}
public IQueryable fGetNewsTags(int-id)
{
var result=来自context.GetTable()中的新闻
其中news.M_Id==Id
选择(ITag)新闻;
返回结果;
}
尝试以下内容

public IQueryable<ITag> fGetNewsTags(int id)
{
    var result = from news in context.GetTable<NewsTag>()
                 where news.M_Id == id
                 select (ITag)news;

    return result;
}
public IQueryable fGetNewsTags(int-id)
{
var result=来自context.GetTable()中的新闻
其中news.M_Id==Id
选择(ITag)新闻;
返回结果;
}