简体   繁体   English

Subsonic3 ActiveRecord LINQ查询帮助

[英]Help with Subsonic3 ActiveRecord LINQ query

I have the following subsonic entities 我有以下亚音速实体

TInvoiceHeader
TAccountAssociation

How can I achieve the following in LINQ (subsonic) 如何在LINQ(亚音速)中实现以下目标

SELECT * from TInvoiceHeader
WHERE custid IN 
  (SELECT custid FROM TAccountAssociation
     WHERE username = 'a')

I need to bind the results to a GridView. 我需要将结果绑定到GridView。

Update: I tried 更新:我试过了

Dim accounts As List(Of TAccountAssociation) = _
TAccountAssociation.Find(Function(x) x.UserName = "a")

        GridView1.DataSource = TInvoiceHeader.All() _
             .Where(Function(x) accounts.Contains(x.custID))
        GridView1.DataBind() 

But I get an error "...nested function dows not have the same signature as delegate" 但是我收到一个错误“ ...嵌套函数的dow与委托的签名不同”

Update: 更新:

I really dont freaking get this... 我真的不怕这个...

why does this work

        Dim accounts() As String = {"N12345", "A12455"}


        GridView1.DataSource = TInvoiceHeader.All(). _
Where(Function(c) accounts.Contains(c.custID))
        GridView1.DataBind()

But this doesn't 但这不是

Dim accounts  = TAccountAssociation.Find(Function(x) x.UserName = "a")

        GridView1.DataSource = TInvoiceHeader.All(). _
Where(Function(c) accounts.Contains(c.custID))
        GridView1.DataBind()

Update 更新资料

I ended up using the Fluent Query 我最终使用了Fluent Query

GridView1.DataSource = New customerWEBDB().Select.From(Of TInvoiceHeader)_
              .Where("custID") _
              .In(New customerWEBDB().SelectColumns("custID") _
              .From(Of TAccountAssociation) _
              .Where("UserName").IsEqualTo("aaa")) _
              .ExecuteTypedList(Of TInvoiceHeader)()

 GridView1.DataBind()

Hopefully someone will show me something better. 希望有人能给我展示一些更好的东西。

Have a look at this 101 linq examples. 看一下 101个linq示例。 There is some great stuff here. 这里有一些很棒的东西。 Also read through Scott Gu's blog - the examples are with Linq to SQL but the LINQ stuff should be very similar. 也请阅读Scott Gu的博客 -示例与Linq to SQL有关,但是LINQ的内容应该非常相似。

You could do something like this: 您可以执行以下操作:

var query = (from IH in db.TInvoiceHeader
              join AA in db.TAccountAssociation on 
               IH.custid equals AA.custid
              where aa.username.equals("a")
              select ID).ToList();

This will work as long as long as ID.custid and aa.custid are the same type (and are both nullable or non nullable). 只要ID.custid和aa.custid是同一类型(并且都可以为空或不可为空),这将起作用。 If this is not the case, you'll need something like this: 如果不是这种情况,则需要这样的东西:

var query = (from IH in db.TInvoiceHeader
                  join AA in db.TAccountAssociation on 
                  new { ID = IH.custid.Value } equals new {ID = AA.custid}
                  where aa.username.equals("a")
                  select ID).ToList();

IH.custid.Value would be used if IH.custid is a nullable type 如果IH.custid是可为空的类型,则将使用IH.custid.Value

You can now bind query directly to a gridview. 您现在可以将查询直接绑定到gridview。

I have not tested this code - and there are several other ways to accomplish your goal. 我尚未测试此代码-还有其他几种方法可以实现您的目标。

Good luck, 祝好运,

Patrick 帕特里克

There is a good answer to subquery's in linq here that will help: 在linq中对子查询有一个很好的答案,这将有所帮助:

how to do subquery in LINQ 如何在LINQ中执行子查询

i have never had to do subqueries with subsonic 3 however i would suggest that anything advanced SQl wise is better to do as a view or a stored proc so that you don't come up against any missing parts of the linq query builder in subsonic (last time i checked there were a few things it didn't fully do yet) 我从来没有用subsonic 3做子查询,但是我建议最好使用任何高级SQl明智的方法作为视图或存储的proc,这样您就不会遇到subsonic中linq查询构建器的任何缺失部分(上次我检查有几件事尚未完全完成)

The answer to your question (but in C#), and using In rather than the join, would be: 问题的答案(但使用C#),并且使用In而不是join,将是:

var q = Db.Select.From<TInvoiceHeader>()
    .Where(TInvoiceHeaderTable.custidColumn)
    .In(Db.SelectColumns(TAccountAssociationTable.custidColumn)
             .From<TAccountAssociation>()
             .Where(TAccountAssociationTable.usernameColumn)
             .IsEqualTo("a")
    );

List<TInvoiceHeader> collection = q.ExecuteTypedList<TInvoiceHeader>();

I have a similar question on SO regarding NotIn: Subsonic 3 ActiveRecord nested select for NotIn bug? 关于NotIn,我也有类似的问题: Subsonic 3 ActiveRecord嵌套选择NotIn错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM