0

我想使用.Join()LINQ 中的方法来执行以下 SQL:

select * from Enquiries e, QuoteLines q where e.EnquiryId = q.EnquiryId and e.AccountNum = '123' and q.LineNumber = 'ABC123'

用于AccountNumLineNumber将动态传递给方法的参数。

我将如何转移它以使用 LINQ.Join()方法?

public ActionResult EnquirySearch(string id)
{
    var enquiries = new List<Enquiries>();    

    if(id.Contains(' '))
    {
        string[] searchArr = id.Split(' ');
        // want to do my LINQ here
        // this will be split so the first element in the array is the AccountNum
        // and the second element in the array is the LineNumber
    }
    else
    {
        enquiries = context.Enquiries.Where(x=>x.QuoteRef.Contains(id) 
        || x.AccountNum.Contains(id) || x.Owner.Contains(id));
    }
    return View(enquiries);
}
4

2 回答 2

2

在表达式查询中表达联接更容易,它类似于:

var query = from e in Enquiries
            join q in QuoteLines on e.EnquiryId equals q.EnquiryId
            where e.AccountNum == accountNum && q.LineNumber == lineNumber
            select new { Enquiry = e, QuoteLine = q };

或者您可以“早期”过滤 - 这会在 LINQ to Objects 中产生影响,但几乎可以肯定不会在 LINQ to SQL / EF 中:

var query = from e in Enquiries
            where e.AccountNum == accountNum
            join q in QuoteLines.Where(qq => qq.LineNumber == lineNumber)
              on e.EnquiryId equals q.EnquiryId
            select new { Enquiry = e, QuoteLine = q };
于 2012-06-27T14:37:18.500 回答
1

使用加入()

var query = context.Enquiries.Where(e=>e.AccountNum == accountNum)
.Join(context.QuoteLines.Where(q=>q.LineNumber==lineNumber),
 e => e.EnquiryID, q => q.EnquiryID,(e,q)=>e);

这似乎更棘手,但它确实有智能感知支持。我不知道第四个参数是如何工作的,但确实如此。

于 2014-02-26T22:00:01.013 回答