Linq Query using OR condition in ASP.Net

nauna
 
on Jul 11, 2019 05:40 AM
Sample_279463.zip
1539 Views

how to write or clause

string country="0"

if country is 0 so all records should show and if any other value pass so filter the data 

 var e = (from ue in db.UserExpertises
          join ex in db.Expertises on ue.ExpertiseId equals ex.ExpertiseId
          //join qq in q on ue.UserName equals qq.username
          join p in db.Profiles on ue.UserName equals p.UserName
          join ce in db.CoachExpertises on p.UserName equals ce.UserName
          where ex.Urlpath == expertise.ToString() &&
          p.Country =="0" || p.Country==country
          select p

 like we used to do it in sQL

AND (ads.brandId = @brandId OR @brandId = 0) 

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
pandeyism
 
on Jul 11, 2019 05:57 AM
on Jul 12, 2019 02:03 AM

Hi nauna,

Refer below code.

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        NorthwindEntities entity = new NorthwindEntities();
        var product = (from prod in entity.Products
                        select prod).ToList();

        var category = (from cate in entity.Categories
                        where cate.CategoryID == 1 || cate.CategoryID == 2
                        select cate).ToList();
        var result;
        if (entity.Products.Any(x => x.CategoryID == 0))
        {
            result = (from p in entity.Products
                     join c in entity.Categories on p.CategoryID equals c.CategoryID
                     select new
                     {
                         CategoryID = p.CategoryID,
                         CategoryName = c.CategoryName,
                         ProductName = p.ProductName
                     });
        }
        else
        {
            result = from p in product
                     join c in category on p.CategoryID equals c.CategoryID
                     select new
                     {
                         CategoryID = p.CategoryID,
                         CategoryName = c.CategoryName,
                         ProductName = p.ProductName
                     };
        }
        GridView1.DataSource = result.ToList().Take(5);
        GridView1.DataBind();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim entity As NorthwindEntities = New NorthwindEntities()
        Dim product = (From prod In entity.Products Select prod).ToList()
        Dim category = (From cate In entity.Categories Where cate.CategoryID = 1 OrElse cate.CategoryID = 2 Select cate).ToList()
        Dim result
        If entity.Products.Any(Function(x) x.CategoryID = 0) Then
            result = (From p In entity.Products Join c In entity.Categories On p.CategoryID Equals c.CategoryID Select New With {Key .CategoryID = p.CategoryID, Key .CategoryName = c.CategoryName, Key .ProductName = p.ProductName})
            GridView1.DataSource = result.ToList().Take(5)
            GridView1.DataBind()
        Else
            result = From p In product Join c In category On p.CategoryID Equals c.CategoryID Select New With {Key .CategoryID = p.CategoryID, Key .CategoryName = c.CategoryName, Key .ProductName = p.ProductName}            
        End If
        GridView1.DataSource = result.ToList().Take(5)
        GridView1.DataBind()
    End If
End Sub