针对 DataContext 和接口的通用函数约束使用 lambda 时出现 NotSupportedException

发布于 2024-08-09 14:24:32 字数 1492 浏览 3 评论 0原文

我不完全确定标题的措辞是否正确,但情况是这样的...今天我在尝试为 Linq to Sql 创建通用保存函数时注意到,当我对数据上下文选择使用 lambda 时。它打破了具有另一个通用接口的类型约束的通用函数。但是,它可以很好地使用 LINQ 语法。我提出这个问题或多或少是为了了解为什么会发生这种情况。

抛出的异常是:
NotSupportedException
不支持接口成员IEntity`1.ID的映射。

涉及的代码看起来与此类似(简化):
导致错误的函数是 GenericFunc

//Assume SomeTable is generated from a DBML L2S file.

//partial extension of the L2S class
public partial class SomeTable : IEntity<SomeTable> { }

//interface to expose shared properties in these entity classes
public interface IEntity<T> where T : class
{
    //isolated application, int Primary key for all these tables.
    int ID { get; set; } 
}

//simple helper class for L2S stuff.
public class Repository
{
    //helper for inserting/updating data..
    public void SaveSomeTable(SomeTable data)
    {
        SomeDataContext db = new SomeDataContext();
        GenericFunc<SomeTable>(db.SomeTables, data);
    }

    //toy function for this example
    public void GenericFunc<T>(System.Data.Linq.Table<T> table, T data) where T : class, IEntity<T>, new ()
    {
        //note the generic type constraints... 
        //in this case, all table entities conform to the IEntity<T> class.

        //breaks
        var row = table.SingleOrDefault(o => o.ID == data.ID); 

        //works
        var row1 = (from item in table where item.ID == data.ID select item).SingleOrDefault();

        //... more stuff.
    }
}

I'm not entirely sure the title is properly worded, but here's the situation... I noticed today while trying to create a generic save function for Linq to Sql that when I use lambda against a data context select. It breaks within a generic function with a type constraint of another generic interface. However, it works fine with LINQ syntax. I'm posing this question more or less to get an understanding of why this happens.

The exception being thrown is:

NotSupportedException

The mapping of interface member IEntity`1.ID is not supported.

The code involved looks similar to this (simplified):

The error causing function is GenericFunc<T>


//Assume SomeTable is generated from a DBML L2S file.

//partial extension of the L2S class
public partial class SomeTable : IEntity<SomeTable> { }

//interface to expose shared properties in these entity classes
public interface IEntity<T> where T : class
{
    //isolated application, int Primary key for all these tables.
    int ID { get; set; } 
}

//simple helper class for L2S stuff.
public class Repository
{
    //helper for inserting/updating data..
    public void SaveSomeTable(SomeTable data)
    {
        SomeDataContext db = new SomeDataContext();
        GenericFunc<SomeTable>(db.SomeTables, data);
    }

    //toy function for this example
    public void GenericFunc<T>(System.Data.Linq.Table<T> table, T data) where T : class, IEntity<T>, new ()
    {
        //note the generic type constraints... 
        //in this case, all table entities conform to the IEntity<T> class.

        //breaks
        var row = table.SingleOrDefault(o => o.ID == data.ID); 

        //works
        var row1 = (from item in table where item.ID == data.ID select item).SingleOrDefault();

        //... more stuff.
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

時窥 2024-08-16 14:24:32

这实际上并不是 lambda 问题。这是也可以工作的非查询语法:

var row1 = table.Where(o => o.ID == data.ID)
                .SingleOrDefault();

基本上编译为与查询表达式相同的代码。

我强烈怀疑这是 SingleOrDefault (内部)的不同代码路径,这会带来问题。

This isn't really a lambda issue. Here's non-query-syntax which will work too:

var row1 = table.Where(o => o.ID == data.ID)
                .SingleOrDefault();

That compiles to the same code as the query expresion, basically.

I strongly suspect it's a different code path for SingleOrDefault (internally) which gives problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文