Linq 查询 连接具有不同数据源的对象?

发布于 2024-09-28 01:11:55 字数 689 浏览 0 评论 0原文

我正在尝试连接两个对象,第一个是在 Helper 中定义的(静态)本地对象,第二个是(亚音速)数据库对象。

这是我的存储库中令人不快的摘录,除非有要求,否则我不会用模型和助手让您感到厌烦。

公共 IQueryable GetData(字符串数据类型) {

        IQueryable<DatabaseObject> datalist = (
            from t in db.All<DatabaseObject>()
            join e in WebHelpers.LocalList.AsQueryable<LocalObject>()
            on t.Type equals e.Type
            orderby t.DateOccurred descending
            select t
        ).Where(e => e.Category == TransType);

        return datalist;
    }

我意识到,通过将此表放入数据库,我的生活可以轻松 1000 倍,并且对于下一个版本,我很可能会这样做。但有没有办法实现我想要做的事情呢?我认为这是(a)我没有返回正确的数据类型,因为视图模型期望 IQueryable 或(b)Subsonic 导致了问题。

I am trying to join two objects, the first is a (static) local object defined in a Helper and the second (Subsonic) Database object.

Here is the offending extract from my repository, I wont bore you with the models and helpers unless requested.

public IQueryable GetData(string DataType)
{

        IQueryable<DatabaseObject> datalist = (
            from t in db.All<DatabaseObject>()
            join e in WebHelpers.LocalList.AsQueryable<LocalObject>()
            on t.Type equals e.Type
            orderby t.DateOccurred descending
            select t
        ).Where(e => e.Category == TransType);

        return datalist;
    }

I realise that I could me my life 1000 times easier by putting this table into the database, and for the next release I may very well do this. But is there a way to achieve what I am trying to do? Im thinking this is either (a) Im not returning the correct datatype as the view model expects IQueryable or (b) Subsonic is causing the issue.

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

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

发布评论

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

评论(2

两相知 2024-10-05 01:11:55

恐怕 SubSonic 不支持这种交叉引用(至少我上次尝试时不支持)。

您可以通过仅使用 SubSonic 从数据库中检索必要的数据来解决此问题,然后在正常的 C# Linq-to-Objects 中执行 Join:(

public IQueryable GetData(string DataType)
{
    // Get a list of the types we need
    var requiredTypes = WebHelpers.LocalList.Select(l => l.Type)
                                  .Distinct().ToArray();

    // Retrieve all the relevant rows from the database
    var dbData = db.All<DatabaseObject>()
                                  .Where(d => requiredTypes.Contains(d.Type))
                                  .ToArray();

    // Do the join locally
    return (
        from t in dbData
        join e in WebHelpers.LocalList
        on t.Type equals e.Type
        orderby t.DateOccurred descending
        select t
    ).Where(e => e.Category == TransType);
}

顺便说一下,如果您移动 .Where() 位于 orderby 之上,因为这样需要排序的内容就更少了,也许您甚至可以将其包含在数据库查询中(检索 dbData 的查询)。 )我不太清楚,因为你的代码很混乱,因为它引用了你没有在任何地方声明的 TransType ,并且该方法有一个参数 DataType ,但它不是在任何地方使用,并且 te 的使用不一致。)

I am afraid SubSonic doesn’t support this kind of cross-referencing (at least it didn’t last time I tried).

You can work around this by using SubSonic only to retrieve the necessary data from the database, but then do the Join in normal C# Linq-to-Objects:

public IQueryable GetData(string DataType)
{
    // Get a list of the types we need
    var requiredTypes = WebHelpers.LocalList.Select(l => l.Type)
                                  .Distinct().ToArray();

    // Retrieve all the relevant rows from the database
    var dbData = db.All<DatabaseObject>()
                                  .Where(d => requiredTypes.Contains(d.Type))
                                  .ToArray();

    // Do the join locally
    return (
        from t in dbData
        join e in WebHelpers.LocalList
        on t.Type equals e.Type
        orderby t.DateOccurred descending
        select t
    ).Where(e => e.Category == TransType);
}

(By the way, you can probably achieve better performance if you move the .Where() to above the orderby because then there is less to sort. Perhaps you can even include it in the database query (the one that retrieves dbData). I can’t quite tell because your code is confusing because it references a TransType which you haven’t declared anywhere, and the method has a parameter DataType which is not used anywhere, and you use t and e inconsistently.)

静赏你的温柔 2024-10-05 01:11:55

我的最终解决方案是重新组织 where 子句,以确保 select 仅返回正确的数据类型 (IQueryable)。在不重新组织 where 子句的情况下,尽管指定了 select t,连接数据仍会通过对象传递。

public IQueryable GetData(string DataType)
{

     IQueryable<DatabaseObject> dbData = (
        from t in db.All<DatabaseObject>().Where(e => e.Category == TransType)
        join e in WebHelpers.LocalList
        on t.Type equals e.Type
        orderby t.DateOccurred descending
        select t
    ); 

    return dbData;
}

My final solution was to reorganise the where clauses to ensure that the select returns only the correct datatype (IQueryable). Without reorganising the where clauses, the join data is still passed through the object despite specifying select t.

public IQueryable GetData(string DataType)
{

     IQueryable<DatabaseObject> dbData = (
        from t in db.All<DatabaseObject>().Where(e => e.Category == TransType)
        join e in WebHelpers.LocalList
        on t.Type equals e.Type
        orderby t.DateOccurred descending
        select t
    ); 

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