添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams Owner = (p.price > 0 ? from q in db.Users select q.Name : from r in db.ExternalUsers select r.Name) It'll be interesting to see if that works... if it does, I'd love to see the TSQL (assuming it does eager loading; for lazy loading, probably not too terrible). Marc Gravell Jan 14, 2009 at 14:12 This should work. The "? :" is translated to a "case" expression and there are subqueries. Amy B Jan 14, 2009 at 14:28 Should isn't does. It be nice to have confirmation that this does work. Very useful if it does. Sam Meldrum Jan 14, 2009 at 15:02 @NithinPaul - probably best if you post a new question, explaining exactly what you're trying to do. Richard Ev Feb 26, 2015 at 13:35

I assume from db that this is LINQ-to-SQL / Entity Framework / similar (not LINQ-to-Objects);

Generally, you do better with the conditional syntax ( a ? b : c) - however, I don't know if it will work with your different queries like that (after all, how would your write the TSQL?).

For a trivial example of the type of thing you can do:

select new {p.PriceID, Type = p.Price > 0 ? "debit" : "credit" };

You can do much richer things, but I really doubt you can pick the table in the conditional. You're welcome to try, of course...

// set up the "main query"
var test = from p in _db.test select _db.test;
// if str1 is not null, add a where-condition
if(str1 != null)
    test = test.Where(p => p.test == str);
 var result = _context.Employees
                .Where(x => !x.IsDeleted)
                .Where(x => x.ClientId > (clientId > 0 ? clientId - 1 : -1))
                .Where(x => x.ClientId < (clientId > 0 ? clientId + 1 : 1000))
                .Where(x => x.ContractorFlag == employeeFlag);
            return result;

If clientId = 0 we want ALL employees,. but for any clientId between 1 and 999 we want only clients with that ID. I was having issues with seperate LINQ statements not being the same (Deleted/Clients filters need to be on all queries), so by add these two lines it works (all be it until we have 999+ clients - which would be a happy re-factor day!!

If you are using LinQ with EF Core, an easy example can be this-

var orderedData = await _dbContext.ModelName
                    .OrderBy(c => c.Name.Length.Length > 4 ? c.Name:c.SuperTerm.Name.IndexOf(searchValue))
                    .ThenBy(t => t.Producer)
                    .TolistAsync();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.