kavir
kavir

Reputation: 35

Search multiple tables with no relation by special value with one query using linq to entity

I have the following linq queries:

var bas = new BaskoolEntities();
var radif = "0011395000505000821";
            var query = (from a in bas.ChekedList
                where a.Radifkolsal == radif
                from b in bas.PrioritedList
                where b.Radifkolsal == radif
                from c in bas.IssuedBills
                where c.Radifkolsal == radif
                select new
                {
                    a,
                    b,
                    c
                }).ToList();

I want find a field in 3 tables with no relations in database by one query using Linq to Entity in C#. I am new in Linq programing an googling it but found no result useful to me. Can anyone give me a solution? thanks in advance.

Upvotes: 0

Views: 59

Answers (1)

Zein Makki
Zein Makki

Reputation: 30052

As stated in your comments, you want to find if there is a result or not one of the tables at least, you can do something like this using Any():

var radif = "0011395000505000821";
var found = (from a in bas.ChekedList
             where a.Radifkolsal == radif
             || bas.PrioritedList.Any(p => p.Radifkolsal == radif)
             || bas.IssuedBills.Any(i => i.Radifkolsal == radif)
             select a).Any();

if you used var result = ..... select a.Radifkolsal you would get the value 0011395000505000821 or null. Which is useless for me unless your goal is to find (Do i have a matching result or not) which is exactly what the above query does.

Upvotes: 1

Related Questions