2 in 1 method ?

0

Potrzebuje Waszej pomocy,musze z tych dwoch metod utworzyc jedna ale w tej drugiej ten foreach mi przeszkadza troche i nie wiem jak to do konca zrobic.

public IEnumerable<string> Get(string publikator,string rok,string numer,string str,string dodatek)
        {
            bool czyAkt = CzyAkt(publikator);
            DataContext db = new DataContext();            
            var doki = from d in db.Doks
                    where
                    d.P180.Substring(0, 3) == publikator                     
                    && d.Zeszyt.P110 == rok &&
                    d.Zeszyt.ZeszytP112s.Select(p => p.P112).Contains(numer) &&                           
                    d.Zeszyt.Nazwa.Substring(13,3) == dodatek
                    select d;
            if (czyAkt)
            {
                doki = from d in doki
                       where d.Pozycja == str
                       select d;
            }
            else
            {
                doki = from d in doki
                       where d.PozycjaBezLiter == str
                       select d;
            }

            return doki.Select(d=>d.P180);            
        }       


//////////////////////////////////////////////////////////////////////////////////////////////////////////
public void Update(string publikator, string rok, string numer, string str, string dodatek)
        {
            bool czyAkt = CzyAkt(publikator);
            DataContext db = new DataContext();
            var doki = from d in db.Doks
                       where
                       d.P180.Substring(0, 3) == publikator
                       && d.Zeszyt.P110 == rok &&
                       d.Zeszyt.ZeszytP112s.Select(p => p.P112).Contains(numer) &&
                       d.Zeszyt.Nazwa.Substring(13, 3) == dodatek
                       select d;
            if (czyAkt)
            {
                doki = from d in doki
                       where d.Pozycja == str
                       select d;
            }
            else
            {
                doki = from d in doki
                       where d.PozycjaBezLiter == str
                       select d;
            }
           
            foreach (var s in doki)
            {
                s.Status = Model.DokStatus.US;
            }                       
            db.SubmitChanges();          
        } 
0

Zapisz tą Update tak:
public void Update(string publikator, string rok, string numer, string str, string dodatek)

{
  Get(publikator,rok,numer,str,dodatek);
  foreach (var s in doki)
  {
     s.Status = Model.DokStatus.US;
  } 
  db.SubmitChanges();          
} 
0
public IEnumerable<string> GetAndUpdate(string publikator,string rok,string numer,string str,string dodatek)
        {
            bool czyAkt = CzyAkt(publikator);
            DataContext db = new DataContext();            
            var doki = from d in db.Doks
                    where
                    d.P180.Substring(0, 3) == publikator                     
                    && d.Zeszyt.P110 == rok &&
                    d.Zeszyt.ZeszytP112s.Select(p => p.P112).Contains(numer) &&                           
                    d.Zeszyt.Nazwa.Substring(13,3) == dodatek
                    select d;
            if (czyAkt)
            {
                doki = from d in doki
                       where d.Pozycja == str
                       select d;
            }
            else
            {
                doki = from d in doki
                       where d.PozycjaBezLiter == str
                       select d;
            }

            foreach (var s in doki)
            {
                s.Status = Model.DokStatus.US;
            }
                       
            db.SubmitChanges();  
   
            return doki.Select(d=>d.P180);            
        }       

O to chodzi? Jeśli tak to nic w tym trudnego nie ma...

0

btw new DataContext() złap sobie w using, bo inaczej gubisz zasoby (połączenia z bazą)

0

Bardziej chcialem cos takiego:

public jakisTyp Wspolna()
{
            bool czyAkt = CzyAkt(publikator);
            DataContext db = new DataContext();            
            var doki = from d in db.Doks
                    where
                    d.P180.Substring(0, 3) == publikator                     
                    && d.Zeszyt.P110 == rok &&
                    d.Zeszyt.ZeszytP112s.Select(p => p.P112).Contains(numer) &&                           
                    d.Zeszyt.Nazwa.Substring(13,3) == dodatek
                    select d;
            if (czyAkt)
            {
                doki = from d in doki
                       where d.Pozycja == str
                       select d;
            }
            else
            {
                doki = from d in doki
                       where d.PozycjaBezLiter == str
                       select d;
            }
} 

Tutaj druga z forachem

public typ Zforeachem()
{
       Wspolna();
//tutaj jakos ten foreach 
db.savechanges();
} 

Tutaj nastepna:

public typ Nastepna()
{
        Wspolna();
         return doki.Select(d=>d.P180);  
} 

Tak mniej wiecej zeby czesc wspolna sie nie powtarzala w 2 miejscach.

1 użytkowników online, w tym zalogowanych: 0, gości: 1