0

I use MVC 4 and There are 5 dropdownlist in my view. when dropdownlist selected text and clicked button search I want to select from 4 table where dropdownlist selected . and if dropdownlist don't selected not checked in condition.

(from met in db.tblMet
                         from useMet in db.tblUseMet.Where(m => m.UseMetID == met.UseMetID_FK).DefaultIfEmpty()
                         from typeMet in db.tblTypeMet.Where(m => m.TypeMetID == met.TypeMetID_FK).DefaultIfEmpty()
                         from mod in db.tblMod.Where(m => m.ModID == met.ModID_FK).DefaultIfEmpty()
                         from affair in db.tblAffairs.Where(m => m.AffairID == met.AffairID_FK).DefaultIfEmpty()
                         from desert in db.tblDeserts.Where(m => m.DesertID == met.DesertID_FK).DefaultIfEmpty()
                         from city in db.tblCities.Where(m => m.CityID == met.CityID_FK).DefaultIfEmpty()
                         from user in db.tblUsers.Where(m => m.UserID == met.UserIDCreate_FK).DefaultIfEmpty()
                         from userCh in db.tblUsers.Where(m => m.UserID == met.UserIDChange_FK).DefaultIfEmpty()
                         from setting in db.tblSettings.Where(m => m.SettingID == met.SettingID_FK).DefaultIfEmpty()
                         from sim in db.tblSims.Where(m => m.SimID == mod.SimID_FK).DefaultIfEmpty()
                         from typemod in db.tblTypeMod.Where(m => m.TypeModID == sim.TypeModID_FK_Kind).DefaultIfEmpty()
                         from gro in db.tblGroupMet.Where(m => m.GroupMetID == met.GroupMetID_FK).DefaultIfEmpty()
                         from group1 in db.tblMetRelateGroups.Where(x => x.MetID_FK == met.MetID).DefaultIfEmpty()
                         from status in db.tblStatus1.Where(m => m.StatusID == met.StatusID_FK).DefaultIfEmpty()

                         where ((city.CityID == City1||city.CityID !=null)
                               && (typeMet.TypeMetID == Type1 || typeMet.TypeMetID != null)
                               && (useMet.UseMetID == Usemeter1|| useMet.UseMetID != null)
                               && (group1.GroupMetID_FK ==Group1 || group1.GroupMetID_FK != null)
                               && (affair.AffairID ==Affair1 || affair.AffairID != null)
                             //|| desert.DesertID==Desert1
                               )

I want to selected dropdownlist selected and if don't select any, not checked in query. in above code select zero record.

4
  • @GertArnold but how? My cod e did not select any record with this condition Commented Jan 2, 2016 at 12:53
  • Maybe you should ask a clear question. Apparently in above code select zero record is a problem statement, not a requirement. Anyway, you better add the conditions dynamically, like so: stackoverflow.com/a/14622200/861716. Commented Jan 2, 2016 at 12:58
  • By the way, your initial error is that it should be || city.CityID == null etc. (not !=). But if you use that code it will build very bad queries. Add the predicates dynamically. Also, I'd strongly recommend to use navigation properties instead of all these verbose joins. Commented Jan 2, 2016 at 13:06
  • @GertArnold Thanks for your help Commented Jan 2, 2016 at 13:11

1 Answer 1

1

You simply use the fluent syntax for this purpose. Here is an example:

var lQuery = from met in db.tblMet;

if (City1 != null)
  lQuery = lQuery.Where(r => r.CityID == City1);

...

This way you can dynamically add conditions to your query.

If you want to add conditions with an logical OR you need a predicate builder, see https://stackoverflow.com/a/1775057/3936440.

Sign up to request clarification or add additional context in comments.

2 Comments

It does, you need a predicate builder in this case, stackoverflow.com/a/1775057/3936440
Yes, you do, but you never said so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.