Good time, it became necessary to refer once to the MS SQL database, and you need to get the result of this type "Field Name, Number of all lines with this field name". using a sequence of calls to extend methods. I was able to generate such code (I have my own methods, comments are nearby). invoiceId - the variable passed to the method

var c = _repository.GetList<client_broker_invoice>(x => x.invoice_id == invoiceId); //получу Iquarable<client_broker_invoice> whare(x => x.invoice_id == invoiceId) //_repository.Query<> вернет: DB.Set<client_broker_invoice>().AsQueryable() var cc = _repository .Query<client_broker_invoice>() .Join( c, i => i.client_broker_id, o => o.client_broker_id, (i, o) => new { brokerId = i.client_broker_id //тут нужно количество записей где brokerId = i.client_broker_id }); 

It is necessary to somehow implement in addition to the sample the number of records where the brokerId is the same. Whether it is possible to make it to a DB. Those. don't do that

 var check = cc.ToList(); var note = new {Id = check.First().brokerId, count = check.Count()}; 

The request is constructed in such a way that it returns 100% records where the brokerId is all the same

PS On sql I presented it like this:

  select c1.client_broker_id as [brId], count(*) from client_broker_invoice c1 join (select c2.client_broker_id from client_broker_invoice c2 where c2.invoice_id = 205331) c3 on c1.client_broker_id = c3.client_broker_id group by c1.client_broker_id 

in this case, invoiceId = 205331

    1 answer 1

    If I understood the question correctly, then something like that?

     var result = (from c in _repository.Query<client_broker_invoice>() where c.invoice_id == invoiceId group c by c.client_broker_id into cg select new { BrokerId = cg.Key, Count = cg.Count() }).ToList(); 

    Or again the same, but without linq syntax:

     var result = _repository.Query<client_broker_invoice>() .Where(c => c.invoice_id == invoiceId) .GroupBy(c => c.client_broker_id) .Select(cg => new { BrokerId = cg.Key, Count = cg.Count() }) .ToList(); 
    • and what in this case will be in cg? Select (cg => new {BrokerId = cg.Key, Count = cg.Count ()}) - simply good
    • The cg will be System.Linq.Lookup <TKey, TElement> .Grouping. It's like a dictionary, with only one set of elements. - aim
    • then cg.Key doesn't roll, but we have a cg dictionary (i.e., many elements) - simply good
    • Only the key and the number of elements with such a key are selected. - aim
    • The cg contains information about one group of elements. In Key, the value of the element by which the grouping was performed. First try the code, then tell whether it turned out what you need or not. - aim