Home > Mobile >  DefaultIfEmpty doesnt apply into whole list but the first object
DefaultIfEmpty doesnt apply into whole list but the first object

Time:11-15

Here is the code:

public class LinqDatabase : MonoBehaviour
{
    [Serializable]
    public class Supplier
    {

        public string Name;
        public string District;
        public int Age;

    }

    [Serializable]
    public class Buyer
    {
        public string Name;
        public string District;
        public int Age;
    }

    [SerializeField] private List<Buyer> _buyers = new List<Buyer>();
    [SerializeField] private List<Supplier> _supplier = new List<Supplier>();

    void Start ( )
    {
        var leftOuterJJoin = from s in _supplier
            join b in _buyers on s.District equals b.District into NewbuyersGroup
            from BG in NewbuyersGroup.DefaultIfEmpty()
            select new
            {
                s.Name, s.District,
                BuyersName = BG?.Name ?? "No one here"
            };

        foreach ( var VARIABLE in leftOuterJJoin )
        {
            Debug.Log ( $"{VARIABLE.Name} {VARIABLE.District}" );
            Debug.Log ( VARIABLE.BuyersName );
        }
    }
}

enter image description here

enter image description here

enter image description here

My question is that if you compare the list, it should print out no one here for S5w and TTW mkm as well since there isn't a match in the district. However, the DefaultIfEmpty doesn't seem to apply to the whole list but only the first unmatch object(S4ko). How do I apply no one here to all unmatch districts?enter image description here

CodePudding user response:

It is working fine.

You have enabled the option Collapse in the Unity Console which means if the exact same message is printed multiple times it is only displayed once but with a little counter.

You can see on the right border that the message

No one here

has been logged 3 times.

=> Disable Collapse and it should work just fine.

See fiddle


In order to avoid this kind of confusion I would make one single log containing all relevant information. E.g. with

Debug.Log($"{VARIABLE.Name} {VARIABLE.District} -> {VARIABLE.BuyersName}");

this would never have occurred in the first place ;)

  • Related