简体   繁体   English

如何做一个linq组

[英]How to do a linq group by

I have a linq query in my aspnet core application that is returning more rows then it should. 我在我的aspnet核心应用程序中有一个linq查询,它返回更多的行然后它应该。 The query is as follows. 查询如下。

public List<csAutoComplete> GetAutoComplete(string Type, string filter)
{
string[] GasCodesnotListed = { "F97", "F98", "F99" };
Model1 = from x in _context.TblGascodes
    where ((x.GasCode.StartsWith("F13") || x.GasName.StartsWith("F13")) &&
            !GasCodesnotListed.Contains(x.FedclusterCode))
    orderby x.GasCode
    group x by new csAutoComplete { ACCode = x.GasCode, ACName = x.GasName } into Alpha
    select new csAutoComplete  <=== (Is this the issue???)
        {
        ACCode = Alpha.Key.ACCode,
        ACName = Alpha.Key.ACCode + " - " + Alpha.Key.ACName
        };
return Model1.ToList();
}

} returns (7) results } return(7)结果

So I pasted into LINQ pad and got the result I expected of (1) 所以我粘贴到LINQ pad并得到了我期望的结果(1)

string[] GasCodesnotListed = { "F97", "F98", "F99" };
TblGasCodes
   .Where (
      x => 
        ((x.GasCode.StartsWith ("F13") || x.GasName.StartsWith ("F13")) && 
           !(GasCodesnotListed.Contains (x.GasCode))
        )
   )
   .OrderBy (x => x.GasCode)
   .GroupBy (
      x => 
         new  
         {
            ACCode = x.GasCode, 
            ACName = x.GasName
         }
   )
   .Select (
      Alpha => 
         new  
         {
            ACCode = Alpha.Key.ACCode, 
            ACName = ((Alpha.Key.ACCode + " - ") + Alpha.Key.ACName)
         }
   )

The only difference seems to be in the new csAutoComplete. 唯一的区别似乎是在新的csAutoComplete中。 If that is a class definition why should that make a difference? 如果这是一个类定义,为什么它会产生影响呢? How do I resolve this. 我该如何解决这个问题。

In your code, there seems to be an important difference in GasCodesnotListed.Contains (one looks at FedclusterCode, the other at GasCode): 在你的代码中,GasCodesnotListed.Contains似乎有一个重要的区别(一个看着FedclusterCode,另一个看着GasCode):

where (x.GasCode.StartsWith ("F13") && !GasCodesnotListed.Contains (x.**FedclusterCode**))

...versus... ...与...

.Where (
  x => 
    (
       x.GasCode.StartsWith ("F13") && 
       !(GasCodesnotListed.Contains (x.**GasCode**))
    )
)

Unless you overrode Equals in csAutoComplete , GroupBy will compare instances of csAutoComplete using Object.ReferenceEquals() or some equivalent. 除非您在csAutoComplete覆盖EqualscsAutoComplete GroupBy将使用Object.ReferenceEquals()或某些等效csAutoComplete来比较csAutoComplete实例。 Two instances with identical GasCode and GasName properties will be two separate group keys. 具有相同GasCodeGasName属性的两个实例将是两个单独的组键。 If you've got seven items coming in, that'll group them in seven groups of one item each. 如果您有七件物品进入,则会将它们分为七组,每组一件。

This is not the case with anonymous types. 匿名类型不是这种情况。 Separate instances with the same values for their properties will be treated as equal by GroupBy . 具有相同属性值的单独实例将被GroupBy视为相等。

var items = new[] { 0, 0 }.Select(n => new { x = n, y = n.ToString() }).ToArray();

var groups = items.GroupBy(x => x).ToArray();

//  Not equal here!
var aretheyequal = items[0] == items[1];

//  But GroupBy isn't fooled: There's only one group
var groupcount = groups.Length;

Use your anonymous type for a group key -- or overload Equals on csAutoComplete if you like to live on the edge. 使用您的匿名类型作为组密钥 - 或者如果您希望生活在边缘,则在csAutoComplete上重载Equals It can cause funny bugs, in my experience (or more accurately, programmers write funny bugs by failing to be mindful of the changes it introduces). 根据我的经验,它可能会导致有趣的错误(或者更准确地说,程序员通过不注意它引入的更改来编写有趣的错误)。 I prefer not to go there. 我不想去那里。

Hence, I think your GetAutoComplete method is OK, except for the group by line, which I would change to this: 因此,我认为您的GetAutoComplete方法是正常的,除了逐行,我将更改为:

group x by new { ACCode = x.GasCode, ACName = x.GasName } into Alpha

The later reference to ACCode = Alpha.Key.ACCode etc. shouldn't need to change, since the property names are the same. 后来对ACCode = Alpha.Key.ACCode等的引用不需要改变,因为属性名称是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM