English 中文(简体)
LINQ Join where Clause
原标题:LINQ Join Where Clause

我在争取加入/加入一个条款时,用一个非常简单的缩略语。

我正试图从tb1中提取产品信息清单,而该清单的原状是 t2,但必须加上三个不同的栏目。

因此,将按以下思路研究一些事项:

SELECT     tb1.*
FROM         tb2 INNER JOIN
                      tb1 ON tb2.Col1 = tb1. Col1 AND tb2.Col2 = tb1. Col2 AND 
                      tb2.Col3 = tb1.Col3
WHERE     (tb2.Col1 = col1) AND (tb2.Col2 = col2) AND (tb2.Col4 = string)

第X栏是主要条款,其范围将作为参数予以通过;所有其他栏目都在此处。

你们如何执行多个条款?

人们非常赞赏地指出了正确方向。

最佳回答

为了加入《准则》中的多个领域,你必须创造新的匿名类型,包括你想要比较的栏目,然后在加入时使用这种匿名类型:

var results = from t1 in context.tb1
              join t2 in context.tb2
              on new { t1.Col1, t1.Col2, t1.Col3 } equals
                  new { t2.Col1, t2.Col2, t2.Col3 }
              where t2.Col1 == col1 && t2.Col2 == col2 && t2.Col4 == someString
              select t1;

这里相当于Lambdaynex:

var results = context.tb1.Join(
                  context.tb2,
                  t1 => new { t1.Col1, t1.Col2, t1.Col3 },
                  t2 => new { t2.Col1, t2.Col2, t2.Col3 },
                  (t1, t2) => new { t1, t2 })
              .Where(o => o.t2.Col1 == col1 
                  && o.t2.Col2 == col2
                  && o.t2.Col4 == someString)
              .Select(o => o.t1);

如你所知,在加入时,询问会通常会更容易读书。

问题回答

你还可以在提到你重新加入的表格时,将《WHERE》条款列入Mlamda syntax。

var query = from pt in dc.ProjectTasks
            join ttab in dc.TimeTaskAssigns on pt.Id equals ttab.ProjectTaskId
            join ttb2 in dc.CMS_TAT_TIMEs.Where(a => a.WIP_STATUS ==  B ) on ttab.CmsTimeUno equals ttb2.TIME_UNO
            select pt;

显而易见的是,这难道不是没有? 找到这一解决办法需要很长时间。

此外,你可以集思广益,使用分卡

var innerGroupJoinQuery2 =
    from category in categories
    join prod in products on category.ID equals prod.CategoryID into prodGroup
    from prod2 in prodGroup
    where prod2.UnitPrice > 2.50M
    select prod2;

参看:





相关问题
using the where clause + new constraint with args?

I have a piece of code that looks like this: public static T CreateSomething<T>(SomeType a) where T : SomeMicrosoftBaseClass, new() { var newElement = new T { SomeProperty = a}; ...

Strange .Where() behaviour. Somebody has an explanation?

Original I don t get why the Where() clause doesn t give me the right results in the last example. It isn t any different is it? Why does C# behaves differently? transactions = IEnumerable<...

Linq To Sql Where Or operator

I need to create a query which checks if a field (string) contains one or more words supplied at run time. Basically I need to be able to ask a WhereOr question. This seems like it should be a ...

C# Beginner: Where has my IList.Where() method gone?

I ve got another simple one (I think) that s stumping me. I have written a method in one of my controls that gets the latest version of a file in a CMS given it s filename (i.e. regardless of what ...

sql simple query

Yesterday my friend asked me a question about this query: select * from user where 1=1 I said that the query is incorrect, but he said it s correct. I don t understand how this query can be correct....

Codeigniter WHERE on "AS" field

I have a query where I need to modify the selected data and I want to limit my results of that data. For instance: SELECT table_id, radians( 25 ) AS rad FROM test_table WHERE rad < 5 ORDER BY rad ...

SEARCH several Tables IN SQL

I m trying to search several tables at once for a search term. My query is: SELECT item.ItemID FROM Inventory.Item item JOIN Inventory.Category catR // each item can be in several ...

热门标签