1

Is it possible to have a conditional operator in linq in asp.net? I have tried this piece of code but it shows:

   The name BusinessInstance and LocalityMaster1 is not in a scope on the left hand side of 'equal' Consider swaping the expression on either side of 'equals'. 

I need the result as BusinessName,(LocalityName/BusinessAddress),CityName. My sql is as follows:

from BusinessInstanceInfoKeys in db.utblYPBusinessInstanceInfoKeys.Where(x=> x.BusinessName.Contains(term))
join CityMaster in db.utblCityMasters
on BusinessInstanceInfoKeys.CityID equals CityMaster.CityID

let Address = BusinessInstanceInfoKeys.LocalityID != null
              ? from LocalityMaster1 in db.utblLocalityMasters
                join BusinessInstance in db.utblYPBusinessInstanceInfoKeys
                  on BusinessInstance.LocalityID equals LocalityMaster1.LocalityID
                select new 
                {
                    LocalityMaster1.LocalityName 
                }
              : BusinessInstanceInfoKeys.BusinessAddress
select new
{
    LocalityName = BusinessInstanceInfoKeys.BusinessName + ",(" + Address + ")" + "," + CityMaster.CityName
};

Can anyone suggest the right way?

4
  • 3
    Can you please edit your sample so it have more readable names... objutblutblYP is somewhat long and hard to read. Commented Feb 5, 2014 at 4:28
  • What do you mean by it is not working? Do you get an error, if so what is it? Or do you get an unexpected result? Commented Feb 5, 2014 at 4:32
  • I have edited the question please refer to it Commented Feb 5, 2014 at 4:41
  • Please include as much details as you can. Like in a bug report include what you expect, what you did and what actually happened. Also include the tech you used. Just Sal and linq narrows out to 3 mainstream tech and another dozen or so vendor implementations. Commented Feb 5, 2014 at 4:44

1 Answer 1

1

To get rid of the error, replace this:

on BusinessInstance.LocalityID equals LocalityMaster1.LocalityID

with this:

on LocalityMaster1.LocalityID equals BusinessInstance.LocalityID

Also, you can include a ternary expression inside of a let, but there may be a couple issues:

  • If you're querying directly against the database (i.e. Entity Framework), I don't know that it will be able to produce meaningful SQL from the ternary expression, so it may throw an exception.

  • Both conditions in the ternary expression must return the same data type. Yours do not appear to.


Based on your comments, and if at least one record will be returned if the query below is run, you can try this. (I'm assuming LocalityName and BusinessAddress are both strings.)

let Address = BusinessInstanceInfoKeys.LocalityID != null
              ? (from LocalityMaster1 in db.utblLocalityMasters
                 join BusinessInstance in db.utblYPBusinessInstanceInfoKeys
                   on BusinessInstance.LocalityID equals LocalityMaster1.LocalityID
                 select LocalityMaster1.LocalityName).FirstOrDefault()
              : BusinessInstanceInfoKeys.BusinessAddress
Sign up to request clarification or add additional context in comments.

3 Comments

it shows like the type of conditional operator cannot be determined because there is no implicit conversion
so what can be the solution to this as I am new to Linq
Returning data tyle is varchar(512) and again it shows Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Linq.IQueryable<AnonymousType#1>' and 'string'

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.