开发者

Linq to Entities Distinct Clause

开发者 https://www.devze.com 2023-02-11 23:53 出处:网络
I want to add a distinct to the code below. I cannot figure out the exact syntax. Thanks in advance. var testdates = (from o in db.FMCSA_ME_TEST_DATA

I want to add a distinct to the code below. I cannot figure out the exact syntax. Thanks in advance.

var testdates = (from o in db.FMCSA_ME_TEST_DATA
                 orderby o.DATE   
              开发者_如何学Go   select new
                 {
                    RequestDate = o.DATE
                 });


Use the Distinct() extension method.

Note that Distinct() may negate the existing orderby (I've noticed this in LINQ to SQL), so you may want to use the OrderBy() method afterwards.

var testdates = (from o in db.FMCSA_ME_TEST_DATA
                 select new
                 {
                     RequestDate = o.DATE
                 }).Distinct().OrderBy(x => x.RequestDate);


var testdates = (from o in db.FMCSA_ME_TEST_DATA
                 orderby o.DATE   
                 select new
                 {
                    RequestDate = o.DATE
                 }).Distinct();

The trick is to wrap your query in parenthesis so you can call the distinct method, which you already did, so all you needed was to tack on the method call at the end.


Seems like this should work:

            var testdates = (
                        from o in db.FMCSA_ME_TEST_DATA
                        orderby o.DATE
                        select new { RequestDate = o.DATE }
                        ).Distinct();

Check this link: http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#distinct2

0

精彩评论

暂无评论...
验证码 换一张
取 消