English 中文(简体)
LINQ;如何以最大日期记录加入?
原标题:LINQ; how to get record with max date with join?
  • 时间:2012-05-08 03:24:47
  •  标签:
  • linq
  • max

我如何在准则准则中这样做?

select
    *
from customer c
left join order o on o.CustomerID = c.CustomerID
where o.OrderDate = (select MAX(OrderDate) from order where CustomerID = o.CustomerID )

由于总是每天只发布一项命令,因此不会担心 du。

我看上去左边是否加入《准则》,但我不知道如何或什么地方把子放在。

var query = from customer in clist
            from order in olist
                .Where(o => o.CustomerID == customer.CustomerID)
            select new {
                customer.CustomerID,
                customer.Name,
                customer.Address,
                Product = order != null ? order.Product : string.Empty
            };

事实:

var query = from customer in clist
            from order in olist
            .Where(o => o.CustomerID == customer.CustomerID && o.OrderDate ==
                olist.Where(o1 => o1.CustomerID == customer.CustomerID).Max(o1 => o1.OrderDate)
            )
            select new {
                customer.CustomerID,
                customer.Name,
                customer.Address,
                order.Product,
                order.OrderDate
            };

另一种没有伊斯兰堡垒的解决办法

var query = from customer in clist
            from order in olist 
            where order.CustomerID == customer.CustomerID && order.OrderDate == 
                (from o in olist 
                 where o.CustomerID == customer.CustomerID 
                 select o.OrderDate).Max()
            select new {
                customer.CustomerID,
                customer.Name,
                customer.Address,
                order.Product,
                order.OrderDate
            };
最佳回答

字面翻译

var query = from customer in clist
            from order in olist
                .Where(o => o.CustomerID == customer.CustomerID &&
                            o.OrderDate == olist
                                .Where(o => o.CustomerID == customer.CustomerID)
                                .Select(o => o.OrderDate).Max())
            select new {
                customer.CustomerID,
                customer.Name,
                customer.Address,
                Product = order != null ? order.Product : string.Empty
            };

但我会改写。

var query = from customer in clist
            from order in olist
                .Where(o => o.CustomerID == customer.CustomerID)
                .OrderByDescending(o => o.OrderDate).Take(1)
            select new {
                customer.CustomerID,
                customer.Name,
                customer.Address,
                Product = order != null ? order.Product : string.Empty
            };
问题回答

你们的工作如何?

var query =
    from customer in clist
    join order in olist
        on customer.CustomerID equals order.CustomerID
        into orders
    select new
    {
        customer.CustomerID,
        customer.Name,
        customer.Address,
        Product = orders
            .OrderByDescending(x => x.OrderDate)
            .Select(x => x.Product)
            .FirstOrDefault() ?? String.Empty
    };




相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签