Linq和EF4,Inner连接条件语句

时间:2012-07-08 22:59:52

标签: asp.net asp.net-mvc-3 linq entity-framework-4

OrdersRepository ordersRepository = new OrdersRepository();

var productInQuery = ordersRepository.ProductIn;
var orderInfoQuery = ordersRepository.OrderInfo;


var result = (from p in productInQuery
              from o in orderInfoQuery
              where p.refNo == o.refNo
              select new {
                 t1 = p.no,
                 t2 = o.no}).ToArray();

我为上连接写了上面的代码。我想为OrderInfo上下文添加一些条件语句

...

if(orderDate != DateTime.MinValue){
   orderInfoQuery = orderInfoQuery.Where(x => x.orderDate == orderDate); 
}

// also I tried this too. no is integer in Mysql and I'm sure there is 222 value.
// but still it return always null...
orderInfoQuery = orderInfoQuery.Where(x => x.no == 222);



var result = (from p in productInQuery
                  from o in orderInfoQuery
                  where p.refNo == o.refNo
                  select new {
                     t1 = p.no,
                     t2 = o.no}).ToArray();

我添加条件语句但它返回始终为null 。 (我检查DB(Mysql)值是否存在)

是不是内部联接

var result = (from p in productInQuery
              join o in orderInfoQuery on p.refNo equals o.refNo
              select new {
                 t1 = p.no,
                 t2 = o.no}).ToArray();

任何人都知道这个,请指教我〜

谢谢!

[编辑]

OMG,我很抱歉,productIn和orderInfo永远不会匹配。这就是为什么它总是返回null。

请再问一次问题,

var result = (from p in productInQuery
  join o in orderInfoQuery on p.refNo equals o.refNo
  join x in productQuery on p.productNo equals x.no
  join t in productOutQuery on p.no equals t.productInNo into productIn
  from t in productIn.DefaultIfEmpty()
  orderby o.processDate descending
  select new
  {
      qty = p.qty,
      dateIn = o.processDate,
      dateOut = (DateTime?)(from m in orderInfoQuery where m.refNo == t.refNo select m.processDate).FirstOrDefault(),
      etaDate = (DateTime?)(from w in orderInfoQuery where w.refNo == t.refNo select w.eta).FirstOrDefault(),  
  }).ToArray();

这是我的linq代码,我现在想通过etaDate进行搜索。

如何编写linq代码。在上面的代码中,etaDate只是子查询。我想得到

etaDate与特定日期完全匹配的日期。

你可以帮我吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可能需要确保这两个日期之间的类型:

orderDate != DateTime.MinValue

在这里:

x.orderDate == orderDate

采用相同的日期格式。如果一个值具有不同的格式或增加额外的精度,则条件将始终失败。